[英]android get all external storage path for all devices
getExternalStorageDirectory()
return SD card path on my phone. getExternalStorageDirectory()
在我的手机上返回SD 卡路径。 ( Huawei Y320 - android 4.2.2 ). (华为 Y320 - 安卓 4.2.2 )。
now, how to get path Phone Storage path for all devices and all API?现在,如何获取所有设备和所有 API 的路径电话存储路径? loot at bottom screenshot.战利品底部截图。
External directory can better be thought as media/shared storage.外部目录最好被认为是媒体/共享存储。 It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions).它是一个文件系统,可以保存相对大量的数据,并在所有应用程序之间共享(不强制执行权限)。 Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.传统上这是一个 SD 卡,但它也可以作为设备中的内置存储实现,该设备与受保护的内部存储不同,可以作为文件系统安装在计算机上。
if you User data directory...如果您的用户数据目录...
Environment.getDataDirectory()
Recommended reading : Android External Storage推荐阅读: Android 外置存储
Cheers!干杯!
try this code to get all external storage path for all devices尝试使用此代码获取所有设备的所有外部存储路径
File[] f = ContextCompat.getExternalFilesDirs(getApplicationContext(),null);
for (int i=0;i< f.length;i++)
{
String path = f[i].getParent().replace("/Android/data/","").replace(getPackageName(),"");
Log.d("DIRS",path); //sdcard and internal and usb
}
Android 21+:安卓 21+:
val publicStorages = ContextCompat.getExternalFilesDirs(this, null).mapNotNull {
it?.parentFile?.parentFile?.parentFile?.parentFile
}
// paths:
// /storage/emulated/0
// /storage/12F7-270F
Android 29+:安卓 29+:
val volumeNames = MediaStore.getExternalVolumeNames(context).toTypedArray()
val phoneSdCard: String = volumeNames[0]
// external_primary == /storage/emulated/0
val removableMicroSdCard: String = volumeNames[1]
// 12f7-270f == /storage/12F7-270F
more about 29+: MediaStore alternative for deprecated Context.externalMediaDirs?更多关于 29+:已弃用的 Context.externalMediaDirs 的 MediaStore 替代方案?
I'm using this method:我正在使用这种方法:
public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";
private static final String ENV_SECONDARY_STORAGE = "SECONDARY_STORAGE";
public static Map<String, File> getAllStorageLocations() {
Map<String, File> storageLocations = new HashMap<>(10);
File sdCard = Environment.getExternalStorageDirectory();
storageLocations.put(SD_CARD, sdCard);
final String rawSecondaryStorage = System.getenv(ENV_SECONDARY_STORAGE);
if (!TextUtils.isEmpty(rawSecondaryStorage)) {
String[] externalCards = rawSecondaryStorage.split(":");
for (int i = 0; i < externalCards.length; i++) {
String path = externalCards[i];
storageLocations.put(EXTERNAL_SD_CARD + String.format(i == 0 ? "" : "_%d", i), new File(path));
}
}
return storageLocations;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.