我在访问三星Galaxy S7上的SD卡时遇到问题。
我在清单中添加了权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />我尝试访问SD卡的目的是使用以下命令获取绝对路径:
Environment.getExternalStorageDirectory().getAbsolutePath()它向我显示了路径: /storage/emulated/0
但通过此路径,我无法访问SD卡上的文件。
我也尝试了不同的答案,包括"external_sd","sdcard",使用"sdcard0","sdcard1",但都不适合我。
然后,我尝试遍历/storage/中的文件夹
File dir = new File("/storage/");
ArrayList<String> fileNames = new ArrayList<String>();
for (File f : dir.listFiles()) {
if (f.isDirectory()){
fileNames.add(f.getName());
}
}
Log.d("file", fileNames.toString());日志显示
[3333-3733, emulated, enc_emulated, self]我发现,我想访问的文件在3333-3733文件夹中。
那么访问SD卡的正确方式是什么呢?
发布于 2017-08-09 18:16:54
所以在最后我使用了这个函数
public File getExternalSdCard() {
File externalStorage = null;
File storage = new File("/storage");
if(storage.exists()) {
File[] files = storage.listFiles();
for (File file : files) {
if (file.exists()) {
try {
if (Environment.isExternalStorageRemovable(file)) {
externalStorage = file;
break;
}
} catch (Exception e) {
Log.e("TAG", e.toString());
}
}
}
}
return externalStorage;
}去拿我的SD卡。
这是非常可悲的。
https://stackoverflow.com/questions/45586448
复制相似问题