简体   繁体   English

Android:在/ dev / video0上设置666权限

[英]Android : Set 666 permissions on /dev/video0

I have built an Android app which uses an external USB camera. 我已经构建了一个使用外部USB摄像头的Android应用。 The camera image is shown full-screen app takes screenshots, and stores them on the local file system. 相机图像显示为全屏应用程序,它会截取屏幕截图,并将其存储在本地文件系统上。 When the USB camera is connected, it creates and streams to the following folder; 连接USB相机后,它会创建并流到以下文件夹;

/dev/video0 / dev / video0

the folder is destroyed each time the camera is disconnected. 每次断开相机连接时,文件夹都会被破坏。

This issue is that my app doesn't have permission to read from this folder. 问题是我的应用没有读取该文件夹的权限。 So I have to set the permissions each time the camera is connected. 因此,每次连接摄像机时,我都必须设置权限。

I have tried to get the app to set the permissions automatically, using the following code; 我尝试使用以下代码让该应用自动设置权限;

Process sh = Runtime.getRuntime().exec(new String[]{"su", "-c", "system/bin/sh"});
sh = Runtime.getRuntime().exec(new String[]{"chmod", "666", "/dev/video0"});        
sh.waitFor();

...which asks the user to grant Super User permissions, and gives me feedback telling me that the user is now root - but I can see via terminal that the permissions haven't been changed on the folder. ...要求用户授予超级用户权限,并给我反馈,告诉我该用户现在是root用户-但我可以通过终端查看该文件夹上的权限尚未更改。

Any help would be greatly appreciated 任何帮助将不胜感激

the shell supports reading commands via stdin (like you are doing with the OutputStream) or as arguments to it's -c command line option. Shell支持通过stdin(就像您对OutputStream所做的那样)或作为其-c命令行选项的参数来读取命令。 For example, this should work also if you'd rather not create the extra object: 例如,如果您不想创建额外的对象,这也应该起作用:

Process sh = Runtime.getRuntime().exec(new String[]{"su", "-c", "/system/bin/sh -c \"chmod 666 /dev/video0\""});

this translates to executing in the shell: 这转化为在shell中执行:

su -c "/system/bin/sh -c \"chmod 666 /dev/video0\""

which you can try out first in adb if you want. 您可以根据需要先在adb中进行尝试。

In this particular case, because you are only executing one command anyway, you don't really need the shell at all and can probably just call this: 在这种特殊情况下,由于无论如何您只执行一个命令,因此您根本不需要外壳程序,并且可以仅调用此命令:

Process sh = Runtime.getRuntime().exec(new String[]{"su", "-c", "chmod 666 /dev/video0"});

Also to add to the comment that you can edit the ueventd.rc file: that should work, but those files are either in the read-only /system partition (which is easily edited by remounting it with read-write) or more likely some ramdisk (gzipped cpio archive) that is flashed to a partition on the device. 还要添加注释,您可以编辑ueventd.rc文件:该文件应该可以工作,但是这些文件位于只读/ system分区(可通过使用读写方式重新安装来轻松编辑)中。闪存到设备上的分区的ramdisk(压缩的cpio存档)。 The latter won't be quite as easy unless you can find that ramdisk image on the flash chip, extract it, edit the file, recreate the cpio, and write it back to the flash. 除非您能在闪存芯片上找到该ramdisk映像,解压缩,编辑文件,重新创建cpio并将其写回到闪存,否则后者将不会那么容易。

If you are not concerned about the risks of giving those permissions to the camera, you can add 如果您不担心将这些权限授予相机的风险,则可以添加

/dev/video*               0666   system     camera

to /ueventd.rc . /ueventd.rc

I eventually found the following code which works great; 我最终发现以下代码非常有效;

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream  os = sh.getOutputStream();
os.write(("chmod 666 /dev/video0").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();

For some reason, the command has to be sent via an OutputStream. 由于某种原因,该命令必须通过OutputStream发送。

也许您只需要在AndroidManifest.xml中添加摄像头权限:

<uses-permission android:name="android.permission.CAMERA"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM