Read/Write Removable SD Card (android emulator)

2019-08-05 01:45发布

问题:

Every time I try to read the informations of SD Card (removable storage), I get android.system.ErrnoException: statvfs failed: EACCES (Permission denied)

I added permissions in manifest file :

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

As 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
        );
    }
}

The method above is called when the activity is create (it shows the popup and I allow the access)

But when I try to access to the device : (Path = /mnt/media_rw/13F4-1C18)

StatFs stat = new StatFs(path);

I got this error :

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) 

Have you any idea please how to allow access to REMOVABLE DEVICE ?

Thanks !

回答1:

SD Card (external storage)

Removable storage is not external storage.

I added permissions in manifest file

Those permissions have nothing to do with removable storage.

I got this error

You do not have direct filesystem access to arbitrary locations on removable storage.

Have you any idea please how to allow access to EXTERNAL SD CARD ?

Either:

  • On Android 4.4+, use getExternalFilesDirs(), getExternalCacheDirs(), and/or getExternalMediaDirs() (note the plural form of the method names) on Android 4.4+, to access directories unique for your app on removable storage. You do not need any permissions to work with these directories.

  • On Android 4.4+, stop thinking in terms of removable storage and use the Storage Access Framework. This allows the user to specify where the content resides, which could be on external storage, removable storage, Google Drive, etc.

  • On Android 7.0+, use StorageManager and its StorageVolume APIs.