gpt4 book ai didi

android - 如何以编程方式获取android6.0中的SD_Card路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:19 24 4
gpt4 key购买 nike

我正在尝试通过使用如下所示的外部存储路径来检查设备是否具有外部存储

 if (new File("/ext_card/").exists()) {
specialPath = "/ext_card/";
} else if (new File("/mnt/sdcard/external_sd/").exists()) {
specialPath = "/mnt/sdcard/external_sd/";
} else if (new File("/storage/extSdCard/").exists()) {
specialPath = "/storage/extSdCard/";
} else if (new File("/mnt/extSdCard/").exists()) {
specialPath = "/mnt/extSdCard/";
} else if (new File("/mnt/sdcard/external_sd/").exists()) {
specialPath = "/mnt/sdcard/external_sd/";
} else if (new File("storage/sdcard1/").exists()) {
specialPath = "storage/sdcard1/";
}

但在 marshmallow 中我找不到这条路径,在使用 ES FILEMANAGER 检查时,它们在 Moto G 3rd generation 中给出了类似 storage/3263-3131 的信息。在检查其他棉花糖设备时,数字会有所不同。请帮我检查棉花糖设备是否有外部存储?如果找到存储意味着如何获取该外部存储的路径?

注意:- 我在我的应用程序中授予了存储权限,还在我的应用程序设置中启用了存储权限。

在此先感谢您,您是否发现我的问题有任何错误,请解决。再次感谢。

最佳答案

这是我的解决方案,保证在 Android 7.0 Nougat 之前有效:

/* returns external storage paths (directory of external memory card) as array of Strings */
public String[] getExternalStorageDirectories() {

List<String> results = new ArrayList<>();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //Method 1 for KitKat & above
File[] externalDirs = getExternalFilesDirs(null);
String internalRoot = Environment.getExternalStorageDirectory().getAbsolutePath().toLowerCase();

for (File file : externalDirs) {
if(file==null) //solved NPE on some Lollipop devices
continue;
String path = file.getPath().split("/Android")[0];

if(path.toLowerCase().startsWith(internalRoot))
continue;

boolean addPath = false;

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
addPath = Environment.isExternalStorageRemovable(file);
}
else{
addPath = Environment.MEDIA_MOUNTED.equals(EnvironmentCompat.getStorageState(file));
}

if(addPath){
results.add(path);
}
}
}

if(results.isEmpty()) { //Method 2 for all versions
// better variation of: http://stackoverflow.com/a/40123073/5002496
String output = "";
try {
final Process process = new ProcessBuilder().command("mount | grep /dev/block/vold")
.redirectErrorStream(true).start();
process.waitFor();
final InputStream is = process.getInputStream();
final byte[] buffer = new byte[1024];
while (is.read(buffer) != -1) {
output = output + new String(buffer);
}
is.close();
} catch (final Exception e) {
e.printStackTrace();
}
if(!output.trim().isEmpty()) {
String devicePoints[] = output.split("\n");
for(String voldPoint: devicePoints) {
results.add(voldPoint.split(" ")[2]);
}
}
}

//Below few lines is to remove paths which may not be external memory card, like OTG (feel free to comment them out)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
for (int i = 0; i < results.size(); i++) {
if (!results.get(i).toLowerCase().matches(".*[0-9a-f]{4}[-][0-9a-f]{4}")) {
Log.d(LOG_TAG, results.get(i) + " might not be extSDcard");
results.remove(i--);
}
}
} else {
for (int i = 0; i < results.size(); i++) {
if (!results.get(i).toLowerCase().contains("ext") && !results.get(i).toLowerCase().contains("sdcard")) {
Log.d(LOG_TAG, results.get(i)+" might not be extSDcard");
results.remove(i--);
}
}
}

String[] storageDirectories = new String[results.size()];
for(int i=0; i<results.size(); ++i) storageDirectories[i] = results.get(i);

return storageDirectories;
}

关于android - 如何以编程方式获取android6.0中的SD_Card路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36766016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com