Android Read Only File System IOException

来源:互联网 发布:新浪微博淘宝卖家认证 编辑:程序博客网 时间:2024/05/16 03:54
最近写了一个截图的功能,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";



0 0