您的位置:首页 > 移动开发 > Android开发

Android Read Only File System IOException

2015-01-25 19:15 344 查看
最近写了一个截图的功能,activity启动之后,截屏。方法如下:
public class ScreenDebug {
public static void screenshot(final View v, final Activity activity) {

new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
activity.runOnUiThread(new Runnable() {
@Override
public void run() {

View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Toast.makeText(activity, "test", Toast.LENGTH_SHORT).show();
if (bitmap != null) {
System.out.println("bitmap got!");
try {
FileOutputStream out = new FileOutputStream(fname);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

Log.d("test", "test");
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}).start();
}


但是在运行之后,却报"java.io.IOException: open failed: EROFS (Read-only file system),最后在stackoverflow上找到答案,是因为现在有些手机不允许我们直接向sd
card上写东西,因此做如下修改:
将String fname = "/sdcard/" + "screenshot.png";
换成String fname = context.getFilesDir().getPath().toString() + "/screenshot.png";
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: