本文介绍了如何在Android中获取sdcard(Secondary)存储路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发文件管理器应用程序.我想向用户显示内部存储和sdcard存储(如果存在).对于内部存储,我使用Environment.getExternalStorageDirectory().getPath()
.如何获取SD存储空间?
I'm developing a File Manager app. I want show to the user the internal storage and the sdcard storage if it exists. For internal storage I use Environment.getExternalStorageDirectory().getPath()
. How can I get the SD storage?
推荐答案
不确定该解决方案是否适合文件管理器,但是经过多年尝试获取正确的代码后,这是我最近的解决方案:
not sure this solution fits a File Manager but after many years trying to get the right code, this is my most recent one:
/**
* Returns all available SD-Cards in the system (include emulated)
*
* Warning: Hack! Based on Android source code of version 4.3 (API 18)
* Because there is no standard way to get it.
*
* @return paths to all available SD-Cards in the system (include emulated)
*/
public static String[] getStorageDirectories(Context pContext)
{
// Final set of paths
final Set<String> rv = new HashSet<>();
//Get primary & secondary external device storage (internal storage & micro SDCARD slot...)
File[] listExternalDirs = ContextCompat.getExternalFilesDirs(pContext, null);
for(int i=0;i<listExternalDirs.length;i++){
if(listExternalDirs[i] != null) {
String path = listExternalDirs[i].getAbsolutePath();
int indexMountRoot = path.indexOf("/Android/data/");
if(indexMountRoot >= 0 && indexMountRoot <= path.length()){
//Get the root path for the external directory
rv.add(path.substring(0, indexMountRoot));
}
}
}
return rv.toArray(new String[rv.size()]);
}
这篇关于如何在Android中获取sdcard(Secondary)存储路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!