读取/写入可移动SD卡(Android仿真器) [英] Read/Write Removable SD Card (android emulator)
问题描述
每次尝试读取SD卡(可移动存储)的信息时,我都会遇到android.system.ErrnoException:statvfs失败:EACCES(权限被拒绝)
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" />
作为minSdkVersion> 23
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)
但是当我尝试访问设备时:(Path = / mnt / media_rw / 13F4-1C18)
But when I try to access to the device : (Path = /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)
您有任何想法请如何允许访问可移动设备?
Have you any idea please how to allow access to REMOVABLE DEVICE ?
谢谢!
推荐答案
SD卡(外部存储)
SD Card (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.
请问您如何允许访问s到外部SD卡吗?
Have you any idea please how to allow access to EXTERNAL SD CARD ?
其中一个:
-
在Android 4.4+上,使用
getExternalFilesDirs()
,getExternalCacheDirs()
和/或< Android 4.4+上的code> getExternalMediaDirs()(注意方法名称的复数形式),以访问可移动存储上您的应用唯一的目录。您不需要任何权限即可使用这些目录。
On Android 4.4+, use
getExternalFilesDirs()
,getExternalCacheDirs()
, and/orgetExternalMediaDirs()
(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.
在Android 4.4+上,停止考虑可移动存储并使用存储访问框架。这样,用户可以指定内容所在的位置,该内容可以位于外部存储,可移动存储,Google云端硬盘等上。
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.
在Android 7.0+上,使用 StorageManager
及其 StorageVolume
API。
On Android 7.0+, use StorageManager
and its StorageVolume
APIs.
这篇关于读取/写入可移动SD卡(Android仿真器)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!