简体   繁体   English

android获取所有设备的所有外部存储路径

[英]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.

相关问题 所有Android设备的通用存储路径 - Common storage path for all Android devices Android获得所有可用的存储设备 - Android get all available storage devices 在Android中使用绝对路径获取内部和外部存储中所有文件(每个文件)的列表 - Get list of all files (every file) in both internal and external storage with absolute path in Android 获取连接到Android设备的所有存储设备的路径列表 - Get the list of paths of all the Storage Devices connected to an Android device 如何从所有 Android 设备上的 uri 获取图像路径 - How to get the image path from uri on all Android devices 我如何在三星和所有其他设备上获得正确的外部存储? - How could i get the correct external storage on Samsung and all other devices? 获取“外部存储”的所有路径的列表时,哪个路径指向内部存储? - Which path point to the internal storage when get a list of all paths of the “external storage”? 如何获取二级存储uri或android中所有设备的二级存储的根目录 - how to get secondary storage uri or root directory of secondary storage of all devices in android Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage 如何在Android中获取所有内存路径(内部或外部内存)列表 - How to Get All Memory Path (Internal or External Memory) list in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM