Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException: /storage/emulated/0/saved_images/grub.jpg: open failed: ENOENT (No such file or directory)

Tags:

android

Am using the below code to save an image in sd card but I keep on getting this below exception

private void SaveImage(Bitmap finalBitmap,String filename) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();

    String fname = filename;
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

Am I missing out anything here?

like image 698
Abx Avatar asked Dec 09 '14 06:12

Abx


1 Answers

If you face this problem in Android version 10 then Open the manifest file and add this line to your application tag.

<application android:requestLegacyExternalStorage="true" .....>

This issue is because of the introduction of scoped storage introduced in Android 10. And make sure that you add permission requests in manifest and take runtime permission from the user. You need runtime permission from the user in latest android versions.

like image 60
Shawon Shams Avatar answered Sep 24 '22 22:09

Shawon Shams