-4

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();
}
Mikko Viitala
  • 8,344
  • 4
  • 37
  • 62
Azharan
  • 21
  • 6
  • it could be either you are missing read external storage permission or */storage/sdcard0/req_images/Image-296.jpg* file is missing – Illegal Argument Nov 28 '14 at 04:19
  • look into this post : http://stackoverflow.com/questions/11620641/android-error-open-failed-enoent – JLONG Nov 28 '14 at 04:21

2 Answers2

0

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

nandeesh
  • 24,740
  • 6
  • 69
  • 79
0

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.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Thank you very much for the answer... i finally don't get any error.. however the i can't find the picture either in gallery nor file manager.. – Azharan Nov 28 '14 at 07:00
  • @Azharan You have create new file again from your bitmap object and save it into sdcard then only you will be able to get it. – GrIsHu Nov 28 '14 at 07:28
  • Make sure you have given write external storage permission in your manifest file. – GrIsHu Nov 28 '14 at 07:40
  • Thank you very much for helping out with this one. Actually the problem is on the external storage permission. I didn't enable it at first. However, i still need the **file.delete();** because if not, only the folder will be created but there is no image in it. Thank you very much again @GrlsHu – Azharan Nov 28 '14 at 07:59
  • @Azharan Glad to help always :) – GrIsHu Nov 28 '14 at 08:06