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

后端 未结 11 1880
萌比男神i
萌比男神i 2020-12-08 22:03

The following code which consists of downloading a file from a server and save it in the storage works fine when the device has an internal storage.
But when I tried it

相关标签:
11条回答
  • 2020-12-08 22:24

    Use READ_EXTERNAL_STORAGE permission to read data from the device.

    0 讨论(0)
  • 2020-12-08 22:24

    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

    0 讨论(0)
  • 2020-12-08 22:27

    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.

    0 讨论(0)
  • 2020-12-08 22:28

    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.

    0 讨论(0)
  • 2020-12-08 22:29

    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.

    0 讨论(0)
  • 2020-12-08 22:31
    try
    Environment.getExternalStorageDirectory().getAbsolutePath()
    

    and don't forget to add

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
提交回复
热议问题