如何在Android中使用unix管道
我需要从 Android 中的应用程序向 C 程序发送一些数据,我考虑使用管道。我读到 Java 可以访问现有管道(并将它们打开为如果它是一个普通文件),但我无法在我的应用程序中做这样的事情。当我尝试时,应用程序只是阻塞,直到出现消息等待关闭,而不在 logcat 上编写任何特殊内容。
我在 Android 邮件列表上发现了帖子关于这个主题,但不是很清楚,它指的是我手机上不存在的文件夹。
此外,我知道不可能在 sdcard 上创建管道,但是当我尝试在 /data
中这样做时,我认为我遇到了根本问题......你知道是否可以访问该管道管道(我尝试进出应用程序文件夹但没有成功)?
我用 mkfifo
创建了管道,并且权限似乎可以由任何用户打开。
prw-rw-rw- root root 2010-11-18 04:53 video_pipe
我尝试添加 X 权限(谁知道......)这是我得到的:
# chmod u+x video_pipe
Bad mode
阻止的代码是 相机初始化(PATH
只是管道的路径):
recorder.setOutputFile(PATH);
这是整个源代码:https://github.com/rbochet/Simple-Camera-App/commits/piped< /a> (提交 22dba257f6)
I need to send some data to a C program from my app in Android, and I think about using pipes. I read that Java can access to existing pipes (and open them as if it's a normal file), but I'm unable to do such a thing in my application. When I try, the app just block until the message wait close appears, without writing anything special on logcat.
I found a thread on android mailing lists about this subject, but it was not very clear, and it refers to a folder that does not exist on my phone.
Furthermore, I know it's not possible to make pipes on the sdcard, but when I try to do so in/data
, I think I have root issues… Do you know if it is possible to access to that pipe (I try in and out of the app folder without success)?
I made the pipe with mkfifo
, and the permissions seems OK to be open by any user.
prw-rw-rw- root root 2010-11-18 04:53 video_pipe
I tried to add the X permission (who knows...) Here is what I have back:
# chmod u+x video_pipe
Bad mode
The code that blocks is the camera initialisation (PATH
is just the path to the pipe):
recorder.setOutputFile(PATH);
Here is the whole source : https://github.com/rbochet/Simple-Camera-App/commits/piped (commit 22dba257f6)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我尝试用现有的最愚蠢的应用程序来解决这个问题。您可以在 github 上找到这个要点。
到目前为止,我发现:
/data/data/package.full.name/
)/data/data/
上执行ls -l -a
并查看组名称)。不要忘记:除非有人在另一边聆听,否则您无法真正在管道中写入。因此,如果你测试我在 github 上发布的文件,你将得到那种 logcat 结果。
在这里,系统暂停了,因为什么也没发生……然后我在手机上运行
cat v_pipe
。这样就完成了。
关闭:当我关闭
OutputStreamWriter
时,监听端(即cat
)结束。如果我注释该行,
cat
仍将等待输入。冲洗:如果你打算在不调用 close 的情况下获得某些东西,这似乎很重要。
回车:使用
\n
。Ok, I tried to fix the problem with the most stupid app that exists. You can find this one as a gist on github.
So far, I discover this :
/data/data/package.full.name/
)ls -l -a
on/data/data/
and have a look to the group name).DO NOT FORGET : You can't actually write in the pipe until someone is listening at the other side. So if you test the file I posted on github, you will have that kind of logcat result.
Here, the system pause because nothing happens… Then I run
cat v_pipe
on the phone.That's done.
closing : when I close the
OutputStreamWriter
, the listening side (iecat
) ends.If I commment the line,
cat
will still wait for input.flushing : seems to be important if you intent to get something without calling close.
Carriage Return : Use
\n
.我认为你可以使用 ParcelFileDescriptor.createPipe()
它将返回一个用于读写的管道数组。欲了解更多信息,请访问开发者网站。
I think you can use
ParcelFileDescriptor.createPipe()
It will return an array of pipe for read and write. For more information, visit the developers website.