Saving bitmap from fragment to internal/external storage [closed]

走远了吗. 提交于 2019-12-13 10:01:42

问题


I was getting the following exception while Saving bitmap from fragment to internal/external storage

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

My aim is to save a drawing from my fragment to my android device. I have been trying to use the same approach in Activity class and it work. However when i try to use it in fragment class, it does not worked.

The app didn't crash it just display this error log.

Here is the code:

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();
}

回答1:


I think you are already deleting the file after creating it and then you are trying to convert the non-exists file into the bitmap which is not valid. That is why you are getting an error no such file or directory found.

Just remove the line file.delete(); from your code and try out.




回答2:


Instead of

   myDir.mkdirs();

use

file.mkdirs();

mkdirs creates only parent directories, so use it on file rather than directory.

Edit: Then maybe add this too

file.createNewFile();



来源:https://stackoverflow.com/questions/27181809/saving-bitmap-from-fragment-to-internal-external-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!