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

查看:1689
本文介绍了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天全站免登陆