简体   繁体   English

如何以编程方式在android中识别可移动存储是OTG?

[英]How to identify removable storage is OTG in android programmatically?

I am trying to get all available storage which is connected to device, In my device total 3 number of storage available which is (Internal storage, SD Card, OTG storage), how can I identify which one is is OTG?, because Internal storage is non removable storage, and SD card and OTG are both removable storage, how to find from removable storage which one is OTG and which one is SD Card?, below is my code.我正在尝试获取连接到设备的所有可用存储空间,在我的设备共有 3 个可用存储空间(内部存储、SD 卡、OTG 存储),我如何确定哪个是 OTG?,因为内部存储是不可移动存储,SD卡和OTG都是可移动存储,如何从移动存储中找到哪个是OTG哪个是SD卡?,下面是我的代码。

fun getAvailableAllStorageVolumes(context: Context): ArrayList<StorageDirectoryParcelable> {
        // Here we gets all storage that are connected to our device
        val volumes = ArrayList<StorageDirectoryParcelable>()
        val sm = context.getSystemService(StorageManager::class.java)

        for (volume in sm.storageVolumes) {
            val path = getVolumeDirectory(volume)
            val name = volume.getDescription(context)
            volumes.add(StorageDirectoryParcelable(path.path, name))

            Log.e("USB","name:- "+ name)
            Log.e("USB","isPrimary:- "+ volume.isPrimary)
            Log.e("USB","isEmulated:- "+ volume.isEmulated)
            Log.e("USB","isRemovable:- "+ volume.isRemovable)
            Log.e("USB","state:- "+ volume.state)
        }
        return volumes
    }   

Try to compare with StorageManager.getPrimaryStorageVolume() it returns the "internal" sdcard volume, so if equals its the sdcard, else its the USB OTG one尝试与StorageManager.getPrimaryStorageVolume()比较它返回“内部”sdcard 卷,所以如果等于它的 sdcard,否则它的 USB OTG 一个

Better you can use isPrimary你可以更好地使用 isPrimary

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM