找到外部SD卡的路径 [英] find path of external SD card
本文介绍了找到外部SD卡的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望获得附加到手机的外部SD路径。
路径如:
mnt / SDCARD0
mnt / extsdcard。
问题在于它与手机不同。
i want to get path of external SD attached to mobile.
path like :
mnt/SDCARD0
mnt/extsdcard.
problem is that it differs from mobile to mobile.
推荐答案
这是一个真正的头痛。所以硬代码路径是不可能的。网上有一些解决方案可以通过编程方式获取路径,但没有任何保证:
1. android-finding-sd-card-path [ ^ ]
2. get-external-sdcard-location-in-android [ ^ ]
That is a true headache. So hard-code path is out of question. There are some solutions on the web to get the path programmatically, but no gaurantee:
1. android-finding-sd-card-path[^]
2. get-external-sdcard-location-in-android[^]
以下代码将有助于...
The below code will helps...
/**
* Returns all available external SD-Card roots in the system.
*
* @return paths to all available external SD-Card roots in the system.
*/
public static String[] getStorageDirectories() {
String[] storageDirectories;
String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
List<string> results = new ArrayList<string>();
File[] externalDirs = myContext.getExternalFilesDirs(null);
for (File file : externalDirs) {
String path = null;
try {
path = file.getPath().split("/Android")[0];
} catch (Exception e) {
e.printStackTrace();
path = null;
}
if (path != null) {
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Environment.isExternalStorageRemovable(file))
|| rawSecondaryStoragesStr != null && rawSecondaryStoragesStr.contains(path)) {
results.add(path);
}
}
}
storageDirectories = results.toArray(new String[0]);
} else {
final Set<string> rv = new HashSet<string>();
if (!TextUtils.isEmpty(rawSecondaryStoragesStr)) {
final String[] rawSecondaryStorages = rawSecondaryStoragesStr.split(File.pathSeparator);
Collections.addAll(rv, rawSecondaryStorages);
}
storageDirectories = rv.toArray(new String[rv.size()]);
}
return storageDirectories;
}
</string></string></string></string>
这篇关于找到外部SD卡的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文