I am trying to store the images captured from camera to public storage directory, here is my part of code for storing image:
protected File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
return image;
}
its working fine in most of the devices, but in some devices i get
java.io.IOException: open failed: ENOENT (No such file or directory)
So my question is why the same piece of code is working fine in most of the devices and why not in some devices and how to fix this issue?
For api 28 and below, you need getExternalStoragePublicDirectory() method but it is deprecated. What if you don't want to use that deprecated method? Then you can use SAF file explorer(Intent#ACTION_OPEN_DOCUMENT).
getExternalStorageDirectory() : return the primary external storage root directory. Context. getExternalFilesDir(String type) : return the absolute path of the directory on the primary external storage where the application can place its own files.
Use context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
instead.
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