java.io.filenotfoundexception open failed eacces (

2020-02-03 06:44发布

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 with a device with no internal storage, only with external storage I get the following exception.

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

public void downloadFile(String dlUrl, String dlName) {
    int count;

    HttpURLConnection con = null;
    InputStream is = null;
    FileOutputStream fos = null;

    try {
        URL url = new URL( dlUrl );
        con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.connect();

        is = url.openStream();
        String dir = Environment.getExternalStorageDirectory() + Util.DL_DIRECTORY;
        File file = new File( dir );
        if( !file.exists() ){
            file.mkdir();
        }

        Util.LOG_W(TAG, "Downloading: " + dlName + " ...");

        fos = new FileOutputStream(file + "/" +  dlName);
        byte data[] = new byte[1024];

        while( (count = is.read(data)) != -1 ){
            fos.write(data, 0, count);
        }

        Util.LOG_D(TAG, dlName + " Download Complete!");


    } catch (Exception e) {
        Util.LOG_E(TAG, "DOWNLOAD ERROR = " + e.toString() );
        bServiceDownloading = false;
    }
    finally{
        try {
            if( is != null)
                is.close();
            if( fos != null)
                fos.close();
            if( con != null)
                con.disconnect();
        } catch (Exception e) {
            Util.LOG_E(TAG, "CLOSE ERROR = " + e.toString() );
        }
    }
}

And in manifest file I has the following:

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

Any suggestions what maybe the cause? By the way Environment.getExternalStorageDirectory() returns /mnt/sdcard/ and file.mkdir() return false.

11条回答
够拽才男人
2楼-- · 2020-02-03 06:57

Use READ_EXTERNAL_STORAGE permission to read data from the device.

查看更多
该账号已被封号
3楼-- · 2020-02-03 06:57

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.

查看更多
女痞
4楼-- · 2020-02-03 06:57

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.

查看更多
\"骚年 ilove
5楼-- · 2020-02-03 07:01

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.

查看更多
ゆ 、 Hurt°
6楼-- · 2020-02-03 07:02

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

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

查看更多
Summer. ? 凉城
7楼-- · 2020-02-03 07:07

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.

查看更多
登录 后发表回答