简体   繁体   English

找不到EACCES权限被拒绝的文件

[英]EACCES Permission denied file not found

I have mounted an external sd card (7.9GB). 我已经安装了外部SD卡(7.9GB)。 Following is the code I am using to transfer a raw audio file from my project to the sdcard. 以下是我用来将原始音频文件从项目传输到sdcard的代码。 I am using JellyBean 4.2 version. 我正在使用JellyBean 4.2版本。 I am able to achieve this using a fileManager app. 我可以使用fileManager应用程序实现此目的。 So the sdcard definitely is writable. 因此sdcard绝对是可写的。

        File storagedir = new File("/mnt/extsd");
        if (storagedir.isDirectory()) {
        String[] dirlist = storagedir.list();
        for (int i = 0; i < dirlist.length; i++) {
            System.out.println(dirlist[i]);
        }
        File file = new File(storagedir, "Audio.mp3");

        try {
            InputStream is = getResources().openRawResource(R.raw.audio);
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();
            Toast.makeText(getApplicationContext(), "Saved!", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
        }

Manifest: 表现:

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

But I get the File not found exception: 但是我得到了File not found的异常:

          java.io.FileNotFoundException: mnt/extsd/Audio.mp3 openfailed:
          EACCES (Permission Denied)

You ideally should not be using such hardcoded paths like that. 理想情况下,您不应使用这样的硬编码路径。 You should be using the strings coming from http://developer.android.com/reference/android/os/Environment.html . 您应该使用来自http://developer.android.com/reference/android/os/Environment.html的字符串。

The main reason for this is because these strings CAN change and its up to the platform to return the correct values. 这样做的主要原因是因为这些字符串可以更改,并且取决于平台,以返回正确的值。

So, it turns out I made a really silly mistake. 因此,事实证明我犯了一个非常愚蠢的错误。 The path should have been: 路径应该是:

               File storagedir = new File("/mnt/extsd/");

   I missed the 2nd backslash after extsd. 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM