Android Read Only File System IOException
16lz
2021-01-23
最近写了一个截图的功能,activity启动之后,截屏。方法如下:
但是在运行之后,却报 "java.io.IOException: open failed: EROFS (Read-only file system), 最后在stackoverflow上找到答案,是因为现在有些手机不允许我们直接向sd card上写东西,因此做如下修改:
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";
更多相关文章
- Android实现屏幕截图并保存截图到指定文件
- Android 整屏截图与区域指定view或layout截屏并返回bitmap对象
- Android 截图程序实现 需要root权限
- Android屏幕截图研究
- Android平台截图
- adb 超快速截图bash脚本
- Android截屏截图的几种方法总结
- Android屏幕截图实现方式 系统截屏源码分析和三指截屏
- Android面试题集(含答案)