无法创建外部文件 - EACCES(权限被拒绝)

时间:2015-04-13 08:33:03

标签: android file-permissions permission-denied android-external-storage

当尝试创建一个简单的.txt或.xml外部文件(不在SD卡上)时,我的应用程序正在抛出一个FileNotFoundException。如果我让系统选择路径(返回/存储/模拟/遗留),则会抛出EACCES(权限被拒绝)。与我手动将路径设置为" / android / data" (return / storage / emulated / 0 / android / data)或" / download" (返回/存储/模拟/ 0 /下载) - 两者都抛出EACCES。如果我将路径设置为" / document"它会抛出ENOENT(没有这样的文件或目录)。

如下面的代码所示,我之前会进行检查,以确保外部存储可用而不是只读。

我在2台设备上运行它,运行4.4.2和4.4.4 我还补充道 " android.permission.MOUNT_UNMOUNT_FILESYSTEMS" 在SO的答案中建议的清单中试图强制设备使用它自己的文件系统而不是PC通过USB运行应用程序,但它似乎继续给我一个源自/的路径模拟/ 我尝试使用开发人员选项从设备本身进行调试,从而消除了应用程序访问PC存储的可能性,但设备上的调试器只是挂起尝试连接。

我当然也有: " android.permission.WRITE_EXTERNAL_STORAGE"             机器人:maxSdkVersion =" 18"

这非常令人沮丧,因为在SO上有一些类似的问题,但是答案非常明确。

public static File getExternalStorageDirectory() {
        Log.i("EXTERNAL STORAGE DIR", EXTERNAL_STORAGE_DIRECTORY.toString());
        return EXTERNAL_STORAGE_DIRECTORY;
    }

    public static final File EXTERNAL_STORAGE_DIRECTORY = getDirectory("EXTERNAL_STORAGE", "android/data");

   public static File getDirectory(String variableName, String defaultPath) {
            String path = System.getenv(variableName);
            return path == null ? new File(defaultPath) : new File(path);
    }

        public boolean isExternalStorageReadOnly() {  
              String extStorageState = Environment.getExternalStorageState();  
              if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {  
               return true;  
              }  
              return false;  
             }  

     public boolean isExternalStorageAvailable() {  
              String extStorageState = Environment.getExternalStorageState();  
              if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {  
               return true;  
              }  
              return false;  
             }  

        public void writeToSettingsFile(String name, String value){
//          File myDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/settings.txt");  
//          File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), fileUrl);
//          File myDir = getDirectory("EXTERNAL_STORAGE", "android/data");
//          File myDir = getExternalStorageDirectory();
//          File myDir = new File("download");
            File myDir = new File(Environment.getExternalStorageDirectory() + "/android/data");
            if(!myDir.exists()){
                myDir.mkdirs();
            }
            try{
             String fname = "settings.txt";
             File file = new File (myDir, fname);
             FileOutputStream fOut = new FileOutputStream(file);
             Toast.makeText(context, "you made it, fOut!!!!", Toast.LENGTH_LONG).show();
             props.setProperty(name, value);     
             props.store(fOut, "Settings Properties");
//           props.storeToXML(fOut, "Settings Properties");                      
             fOut.close();

             } catch (FileNotFoundException e) {
                 e.printStackTrace();
                 Log.e("WTF", "No file found!");

             }  
             catch (IOException e) {e.printStackTrace();
             }  

            }  

2 个答案:

答案 0 :(得分:0)

我认为问题在于你的这一行

android:maxSdkVersion="18" .

原因您正在测试4.4.2。这个设备的api级别为19。

答案 1 :(得分:0)

好的,这是解决方案:

public void writeToSettingsFile(String name, String value){
//          File myDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "/settings.txt");
//          File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), fileUrl);
//          File myDir = getDirectory("EXTERNAL_STORAGE", "android/data");
//          File myDir = getExternalStorageDirectory();
//          File myDir = new File("download");
        File myDir = new File(this.getExternalFilesDir(null), name);
        if(!myDir.exists()){
            myDir.mkdirs();
        }
        try{
            String fname = "settings.txt";
            File file = new File (myDir, fname);
            FileOutputStream fOut = new FileOutputStream(file);

            fOut.write(value.getBytes());

//           props.storeToXML(fOut, "Settings Properties");
            fOut.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.e("WTF", "No file found!");

        }
        catch (IOException e) {e.printStackTrace();
        }

    }

试试这个方法。问题是你无法获得像这样的内部目录

File myDir = new File(Environment.getExternalStorageDirectory() + "/android/data"); // You don't have the permission to do this 

如果你想在内部存储上写,你可以得到这样的目录

File myDir = new File(this.getExternalFilesDir(null), name);

您可以查看For more information about saving files