java.io.filenotfoundexception 在设备上打开失败的 eacces(权限被拒绝) [英] java.io.filenotfoundexception open failed eacces (permission denied) on device

查看:63
本文介绍了java.io.filenotfoundexception 在设备上打开失败的 eacces(权限被拒绝)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当设备具有内部存储器时,以下代码包括从服务器下载文件并将其保存在存储器中.
但是当我在没有内部存储的设备上尝试它时,只有外部存储我得到以下异常.

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 打开失败的 eacces(权限被拒绝)

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"/>

任何建议可能是什么原因?顺便说一下,Environment.getExternalStorageDirectory() 返回 /mnt/sdcard/ 并且 file.mkdir() 返回 false.

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

推荐答案

这个问题似乎是由几个因素引起的.

This problem seems to be caused by several factors.

检查#1
首先在您的清单文件中添加此权限并检查它是否有效:

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>

.....

检查#2:

如果您在模拟器上运行,请检查属性以查看它是否有 SD 卡.

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

检查#3:

禁用从设备到计算机的文件传输.如果启用,应用将无法访问 SD 卡.

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

检查#4:
如果仍然无法正常工作,请尝试以下操作:

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

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

对我来说以下有效:
问题是 getExternalStorageDirectory 返回/mnt/sdcard 而我需要外部存储的实际路径/mnt/sdcard-ext 并且android中没有API可以让我获得可移动sdcard的绝对路径.
我的解决方案是对目录进行硬编码,如下所示:

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.

这篇关于java.io.filenotfoundexception 在设备上打开失败的 eacces(权限被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆