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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With