sdcard - Android4.4上读写外置SD卡出错
阿神
阿神 2017-04-17 17:55:41
0
3
577

Android4.4手机上,在外置SD卡创建目录时,报错如下:

11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err:     at java.io.File.createNewFile(File.java:950)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.transferData(DownloadDispatcher.java:241)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:157)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at com.thin.downloadmanager.DownloadDispatcher.run(DownloadDispatcher.java:99)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at libcore.io.Posix.open(Native Method)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     at java.io.File.createNewFile(File.java:943)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err:     ... 6 more

说明一下我这边的代码。

  1. AndroidManifest中已经声明了读写SD卡的权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  1. 同时,在使用外置SD卡时,已经判断了外置sd卡的加载状态:

    private static boolean isSdAvailable() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
    
        private static String getDir(Context context, String cache) {
        StringBuilder path = new StringBuilder();
        if (isSdAvailable()) {
            // 如果内存卡可以,则存储在内存卡上
            path.append(Environment.getExternalStorageDirectory().getAbsolutePath());
            path.append(File.separator);
            path.append(ROOT);
            path.append(File.separator);
            path.append(cache);
            path.append(File.separator);
        } else {
            // 存放在手机内存中
            File fileDir = context.getCacheDir();
            path.append(fileDir.getAbsolutePath());
            path.append(File.separator);
            path.append(cache);
            path.append(File.separator);
        }

        return path.toString();
    }

求指导一下原因。

补充:我这边找到了出问题的手机,从手机状态来看,是手机的SD卡权限设置错误导致的。代码本身已经是没有问题,故关闭该问题。

阿神
阿神

闭关修行中......

reply all(3)
PHPzhong

The accident was caused by the problem of mobile phone SD path permissions.

伊谢尔伦

After Android 4.4 version, the permissions of third-party applications to read and write external SD cards are restricted;
If you want to write files on the external sd card, the path must be under "Android/data/application package name"

Ty80

I have encountered this problem in a virtual machine with the same code. After switching to a real machine, there is no such problem. Try adding this to wake up from sleep;
<uses-permission android:name="android.permission.WAKE_LOCK" / >

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!