java.io.filenotfoundexception open failed eacces (permission denied) on device

Solution 1:

This attribute is "false" by default on apps targeting Android 10 or higher.

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

Solution 2:

This problem seems to be caused by several factors.

Check#1
First add this permission in your manifest file and check if it is working:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
    ...

</application>

  .....

Check#2:

If you are running on an emulator, check the properties to see if it has an SD card.

Check#3:

Disable file transfer from device to computer. If Enabled, the app wont be able to access the SD card.

Check#4:
If still not working, try the following:

 String dir = Environment.getExternalStorageDirectory().getAbsolutePath()

For me the following worked:
The problem is that getExternalStorageDirectory returns /mnt/sdcard whereas I need the actual path of external storage which is /mnt/sdcard-ext and there is no API in android that can get me the absolute path of removable sdcard.
My solution was to hard code the directory as follows:

String dir = "/mnt/sdcard-ext" ;

Since the application is intended to work only on one device, the above did the job.
If you encounter the same problem, use an file explorer application to find out the name of the external directory and hard code it.

Solution 3:

Use READ_EXTERNAL_STORAGE permission to read data from the device.

Solution 4:

Did you try it on emulator? Check the properties if it has an SD card. I had the same problem, and it was because the emulator did not have an SD card. Check if yours has or not.