每次我尝试读取 SD 卡(可移动存储)的信息时,我都会得到 android.system.ErrnoException: statvfs failed: EACCES (Permission denied)
我在清单文件中添加了权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
当 minSdkVersion > 23
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
创建活动时调用上面的方法(它显示弹出窗口并且我允许访问)
但是当我尝试访问设备时:(路径 = /mnt/media_rw/13F4-1C18)
StatFs stat = new StatFs(path);
我收到了这个错误:
java.lang.IllegalArgumentException: Invalid path: /mnt/media_rw/13F4-1C18
at android.os.StatFs.doStat(StatFs.java:46)
at android.os.StatFs.<init>(StatFs.java:39)
.....
Caused by: android.system.ErrnoException: statvfs failed: EACCES (Permission denied)
at libcore.io.Posix.statvfs(Native Method)
at libcore.io.BlockGuardOs.statvfs(BlockGuardOs.java:304)
at android.system.Os.statvfs(Os.java:506)
at android.os.StatFs.doStat(StatFs.java:44)
at android.os.StatFs.<init>(StatFs.java:39)
您知道如何允许访问 REMOVABLE DEVICE 吗?
谢谢 !