24

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?

Abx
  • 2,852
  • 4
  • 30
  • 50
  • 1
    I fixed the same error message by requesting permission from the user. You have to do it if you are using **SDK > 22**. You can find the full code in [one of the answers here](https://stackoverflow.com/questions/7887078/android-saving-file-to-external-storage#51611692). – João Cartucho Jul 31 '18 at 10:58

6 Answers6

17

Modify your code, as you are not creating the directory:

 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 (); 
    file.createNewFile();
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
humna
  • 189
  • 5
17

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.

5

Starting with SDK version 30, you can NOT extend android:requestLegacyExternalStorage="true". Instead modify your ImageLoader library a little. I see you have to modify your file path: File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "saved_images"); into something like File dir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "saved_images");. This should do a trick.

Vaidas
  • 1,297
  • 1
  • 13
  • 14
2

This is how I read a file from storage when I was checking for a timestamp in a text file. Line 2 is probably the best bet for you.

File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/path");
dir.mkdirs();
File file = new File(dir, ".storage.txt");
Reader pr;
String line = "";
try {
    pr = new FileReader(file);
    int data = pr.read();
        while (data != -1) {
            line += (char) data;
            data = pr.read();
        }
    pr.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
//do stuff with line
Eli Rising
  • 485
  • 1
  • 3
  • 15
1

From Android 6.0.0 you need to use this code :

if (ContextCompat.checkSelfPermission(thisActivity,
        Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
Hamza Zdak
  • 41
  • 2
0

make sure you placed the permissions on Manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
15412s
  • 3,298
  • 5
  • 28
  • 38