首页 > 其他分享 >Android开发中,获取U盘或SD卡路径。

Android开发中,获取U盘或SD卡路径。

时间:2023-01-11 16:34:47浏览次数:38  
标签:VolumeInfo U盘 invoke getMethod val StorageManager Android Class SD

@SuppressLint("PrivateApi")

    private String getStoragePath(Context context, boolean isUsb){

        String path="";

        StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

        Class<?> volumeInfoClazz;

        Class<?> diskInfoClaszz;

        try {

            volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo");

            diskInfoClaszz = Class.forName("android.os.storage.DiskInfo");

            Method StorageManager_getVolumes=Class.forName("android.os.storage.StorageManager").getMethod("getVolumes");

            Method VolumeInfo_GetDisk = volumeInfoClazz.getMethod("getDisk");

            Method VolumeInfo_GetPath = volumeInfoClazz.getMethod("getPath");

            Method DiskInfo_IsUsb = diskInfoClaszz.getMethod("isUsb");

            Method DiskInfo_IsSd = diskInfoClaszz.getMethod("isSd");

            List<Object> List_VolumeInfo = (List<Object>) StorageManager_getVolumes.invoke(mStorageManager);

            assert List_VolumeInfo != null;

            for(int i=0; i<List_VolumeInfo.size(); i++){

                Object volumeInfo = List_VolumeInfo.get(i);

                Object diskInfo = VolumeInfo_GetDisk.invoke(volumeInfo);

                if(diskInfo==null)continue;

                boolean sd= (boolean) DiskInfo_IsSd.invoke(diskInfo);

                boolean usb= (boolean) DiskInfo_IsUsb.invoke(diskInfo);

                File file= (File) VolumeInfo_GetPath.invoke(volumeInfo);

                if(isUsb == usb){//usb

                    assert file != null;

                    path=file.getAbsolutePath();

                }else if(!isUsb == sd){//sd

                    assert file != null;

                    path=file.getAbsolutePath();

                }

            }

        } catch (Exception e) {

            YYLog.print(TAG, "[——————— ——————— Exception:"+e.getMessage()+"]");

            e.printStackTrace();

        }

        return path;

    }

 
private fun getStoragePath(context: Context, isUsb: Boolean): String? {
var path = ""
val mStorageManager: StorageManager =
context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
val volumeInfoClazz: Class<*>
val diskInfoClaszz: Class<*>
try {
volumeInfoClazz = Class.forName("android.os.storage.VolumeInfo")
diskInfoClaszz = Class.forName("android.os.storage.DiskInfo")
val StorageManager_getVolumes: Method =
Class.forName("android.os.storage.StorageManager").getMethod("getVolumes")
val VolumeInfo_GetDisk: Method = volumeInfoClazz.getMethod("getDisk")
val VolumeInfo_GetPath: Method = volumeInfoClazz.getMethod("getPath")
val DiskInfo_IsUsb: Method = diskInfoClaszz.getMethod("isUsb")
val DiskInfo_IsSd: Method = diskInfoClaszz.getMethod("isSd")
val List_VolumeInfo = (StorageManager_getVolumes.invoke(mStorageManager) as List<Any>)
for (i in List_VolumeInfo.indices) {
val volumeInfo = List_VolumeInfo[i]
val diskInfo: Any = VolumeInfo_GetDisk.invoke(volumeInfo) ?: continue
val sd = DiskInfo_IsSd.invoke(diskInfo) as Boolean
val usb = DiskInfo_IsUsb.invoke(diskInfo) as Boolean
val file: File = VolumeInfo_GetPath.invoke(volumeInfo) as File
if (isUsb == usb) { //usb
assert(file != null)
path = file.getAbsolutePath()
} else if (!isUsb == sd) { //sd
assert(file != null)
path = file.getAbsolutePath()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
return path
}
   
fun getUsbDrivePath(mContext: Context): String? {
val mStorageManager = mContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager
var storageVolumeClazz: Class<*>? = null
try {
storageVolumeClazz = Class.forName("android.os.storage.StorageVolume")
val getVolumeList = mStorageManager.javaClass.getMethod("getVolumeList")
val getPath = storageVolumeClazz.getMethod("getPath")
val isRemovable = storageVolumeClazz.getMethod("isRemovable")
val result = getVolumeList.invoke(mStorageManager)
val length = Array.getLength(result)
for (i in 0 until length) {
val storageVolumeElement = Array.get(result, i)
val path = getPath.invoke(storageVolumeElement) as String
val removable = isRemovable.invoke(storageVolumeElement) as Boolean
return if (removable){
path
}else{
null
continue
}
}
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: InvocationTargetException) {
e.printStackTrace()
} catch (e: NoSuchMethodException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}

return null
}

标签:VolumeInfo,U盘,invoke,getMethod,val,StorageManager,Android,Class,SD
From: https://www.cnblogs.com/xgjblog/p/17044182.html

相关文章