我试图用程序截图Android屏幕。我做了以下代码:
private void getsnap(){
try{
Process sh = Runtime.getRuntime().exec("su", null, null);
OutputStream os = sh.getOutputStream();
String filePath = this.getFilesDir().getPath().toString() + "/fileName1.jpeg";
os.write(("/system/bin/screencap -p " + filePath).getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();
}
catch (Exception e)
{
e.printStackTrace();
}
}
java.io.IOException: write failed: EPIPE (Broken pipe)
有人能帮忙吗?我已经查看了其他帖子,但没有发现任何解决我问题的方法。
编辑:
请注意,错误发生在os.write()
行。
EPIPE问题通常发生在您尝试在没有根权限的设备上执行需要根权限(getRuntime().exec
)的命令或同时运行多个根命令时。如果你在模拟器上工作,需要root它,我认为你可以在模拟器运行时尝试:
adb shell mount -o rw,remount -t yaffs2 /dev/block/mtdblock03 /system
adb push su /system/xbin/su
adb shell chmod 06755 /system
adb shell chmod 06755 /system/xbin/su
在这里http://abd-tech.blogspot.com/2011/05/test-root-apps-on-android-emulator.html更详细的解释。
问题是您的应用程序没有访问surface flinger(使用屏幕缓冲区和hw解码器来渲染视频文件)的系统权限。为了拥有这些权限,你必须将你的应用程序构建(并签名)为系统应用程序,并将其定位在system/priv应用程序文件夹中。除此之外,您必须将以下权限添加到此系统应用程序:
<manifest package="com.yourapp.demo"
android:versionCode="1"
coreApp="true"
android:sharedUserId="android.uid.media"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
注意coreApp="true"
android:sharedUserId="android.uid.media"
部分。
并且您必须添加<uses-permission android:name="android.permission.ACCESS_SURFACE_FLINGER" />
权限。
请同时签出:拒绝权限:Can';t访问SurfaceFlinger