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

后端 未结 11 1879
萌比男神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: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:

    
    
        ...
    
    
    

      .....

    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.

提交回复
热议问题