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

我与影子孤独终老i 提交于 2019-11-28 21:27:56

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.

Use READ_EXTERNAL_STORAGE permission to read data from the device.

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.

I had the same problem, and i solved it by disabling file transfer from device to computer. Because if u enable file transfer, sd card is not accessible to debugging application.

try
Environment.getExternalStorageDirectory().getAbsolutePath()

and don't forget to add

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

I suspect you are running Android 6.0 Marshmallow (API 23) or later. If this is the case, you must implement runtime permissions before you try to read/write external storage. https://developer.android.com/training/permissions/requesting.html

Try using mkdirs instead of mkdir. If you are creating a directory path and parent doesn't exist then you should use mkdirs.

gaby

I had the same problem. I used the write and read permission in the manifest correctly , yet it didn't work! The solution was very silly: unplug your phone from the PC before running the application. It seems when your phone is connected as "Mass storage" to the PC, the application cannot access the external storage.

i have done very silly mistake. I have already put in AndroidManifest.xml

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

and also add permission in java file,as get permission pragmatically. But there is mistake of Manifest.permission.READ_EXTERNAL_STORAGE. Please use Manifest.permission.WRITE_EXTERNAL_STORAGE.

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, requestCode);

It is possible that the device requires that you manually give the permissions to the cell phone, check your equipment and check that the write permission is activated on your device.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!