将片段中的位图保存到内部/外部存储
时,我遇到以下异常11-28 12:09:18.667: W/System.err(2620): java.io.FileNotFoundException: /storage/sdcard0/req_images/Image-296.jpg: open failed: ENOENT (No such file or directory)
11-28 12:09:18.717: W/System.err(2620): at libcore.io.IoBridge.open(IoBridge.java:427)
11-28 12:09:18.717: W/System.err(2620): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
11-28 12:09:18.717: W/System.err(2620): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
11-28 12:09:18.717: W/System.err(2620): at com.example.multipletab5.AnswerFragment.saveimage(AnswerFragment.java:133)
11-28 12:09:18.717: W/System.err(2620): at com.example.multipletab5.AnswerFragment.access$0(AnswerFragment.java:112)
11-28 12:09:18.717: W/System.err(2620): at com.example.multipletab5.AnswerFragment$2.onClick(AnswerFragment.java:81)
11-28 12:09:18.717: W/System.err(2620): at android.view.View.performClick(View.java:4147)
11-28 12:09:18.717: W/System.err(2620): at android.view.View$PerformClick.run(View.java:17161)
11-28 12:09:18.717: W/System.err(2620): at android.os.Handler.handleCallback(Handler.java:615)
11-28 12:09:18.717: W/System.err(2620): at android.os.Handler.dispatchMessage(Handler.java:92)
11-28 12:09:18.717: W/System.err(2620): at android.os.Looper.loop(Looper.java:213)
11-28 12:09:18.717: W/System.err(2620): at android.app.ActivityThread.main(ActivityThread.java:4787)
11-28 12:09:18.717: W/System.err(2620): at java.lang.reflect.Method.invokeNative(Native Method)
11-28 12:09:18.717: W/System.err(2620): at java.lang.reflect.Method.invoke(Method.java:511)
11-28 12:09:18.717: W/System.err(2620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
11-28 12:09:18.717: W/System.err(2620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
11-28 12:09:18.717: W/System.err(2620): at dalvik.system.NativeStart.main(Native Method)
11-28 12:09:18.727: W/System.err(2620): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-28 12:09:18.727: W/System.err(2620): at libcore.io.Posix.open(Native Method)
11-28 12:09:18.727: W/System.err(2620): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-28 12:09:18.727: W/System.err(2620): at libcore.io.IoBridge.open(IoBridge.java:411)
11-28 12:09:18.727: W/System.err(2620): ... 16 more
我的目标是将我的片段中的绘图保存到我的Android设备中。我一直在尝试在Activity类中使用相同的方法,它工作。但是,当我尝试在片段类中使用它时,它不起作用。
应用程序没有崩溃它只显示此错误日志。
以下是代码:
View content = v;
content.setDrawingCacheEnabled(true);
bitmap = content.getDrawingCache();
File file;
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
file = new File(myDir, fname);
//Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
而不是
myDir.mkdirs();
使用
file.mkdirs();
mkdirs只创建父目录,因此请在文件而不是目录中使用它。
编辑:然后也可以添加
file.createNewFile();
答案 1 :(得分:0)
我认为您在创建文件后已经删除了该文件,然后您尝试将不存在的文件转换为无效的位图。这就是为什么你收到错误没有找到这样的文件或目录。
只需从代码中删除行file.delete();
即可试用。