Android Mkdir Eacces 权限被拒绝

Android Mkdir Eacces Permission Denied

我需要帮助 mkdir failed: EACCES (Permission denied)

我的构建设置; minSdkVersion 15 - targetSdkVersion 19 - compileSdkVersion 25 - buildToolsVersion "25.0.0"

我使用 Android Studio 2.3

调试设备:三星 J1 (SM-J200F) Android 5.1.1 Android 安全补丁级别 2016-12-01

索尼 Xperia Z5 (E6603) Android 7.0 Android 安全补丁级别 2016-12-01

但是 Google Play APK 版本正在运行

Android 清单;

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

调试日志;

    Line 124 : `FileOutputStream out = new FileOutputStream(file);`

    `03-23 10:59:06.096 24568-24788/com.myAppName I/System.out: AsyncTask #1 calls detatch()
    03-23 10:59:06.106 24568-24568/com.myAppName W/System.err: mkdir failed: EACCES (Permission denied) : /storage/emulated/0/MyAppName
    03-23 10:59:06.106 24568-24568/com.myAppName D/Failed: Open
    03-23 10:59:06.106 24568-24568/com.myAppName W/System.err: java.io.FileNotFoundException: /storage/emulated/0/MyAppName/MyAppName-6662.jpg: open failed: ENOENT (No such file or directory)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.SaveImage(FileDownloadHelper.java:124)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.onPostExecute(FileDownloadHelper.java:59)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.myAppName.helper.FileDownloadHelper.onPostExecute(FileDownloadHelper.java:27)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask.finish(AsyncTask.java:636)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask.access0(AsyncTask.java:177)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.os.Looper.loop(Looper.java:145)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6946)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.Posix.open(Native Method)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
    03-23 10:59:06.111 24568-24568/com.myAppName W/System.err:  ... 15 more`

我的保存功能;

    private Bitmap SaveImage(Bitmap result) {
        String filename = "";
        File instaFolder = new File(Environment.getExternalStorageDirectory().toString()
                + "/" + Constants.FOLDER_NAME);
        if (!instaFolder.exists())
            instaFolder.mkdir();

        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        filename = Constants.FOLDER_NAME + "-" + n + ".jpg";
        file = new File(instaFolder, filename);
        if (file.exists()) file.delete();
        _mImageUrl = file.getPath();
        try {
            FileOutputStream out = new FileOutputStream(file);
            result.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

试试这个:

检查 mkdir() returns 是否成功..并删除 .toString()

 File folder = new File(Environment.getExternalStorageDirectory() +  "/" + Constants.FOLDER_NAME);
boolean success = true;
if (!folder.exists()) {
    success = folder.mkdir();
}
if (success) {
    Log.d("Success","Open");
    // Do work on success
} else {
     Log.d("Failed","Open");
    // Do work else on failure 
}