获取U盘里面的图片文件

需要开通系统权限android:sharedUserId=“android.uid.system”

1.判断U盘是否挂载

const val TYPE_STORAGE_USB = 2
fun checkStorageIsMount(ctx: Context, storageType: Int = TYPE_STORAGE_USB): Boolean {
        var isMounted = false
        val sm: StorageManager = ctx.getSystemService(Context.STORAGE_SERVICE) as StorageManager
        val volumeList = SysApiInvoke.getVolumeList(sm)
        val storagePrefix: String = if (storageType == TYPE_STORAGE_SD) {
            lastSDPrefix =
                SysApiInvoke.getSysProp(SDC_PREFIX)
            lastSDPrefix
        } else {
            lastUsbPrefix =
                SysApiInvoke.getSysProp(USB1_PREFIX)
            lastUsbPrefix
        }
        for (volume in volumeList) {
            if (volume.isRemovable) {
                val usbPath: String =
                    SysApiInvoke.getVolumePath(volume)
                if (SysApiInvoke.getVolumeState(
                        sm,
                        usbPath
                    ) == Environment.MEDIA_MOUNTED) {
                    val prefix = usbPath.substring(usbPath.lastIndexOf("/") + 1)
                    if (prefix == storagePrefix) {
                        isMounted = true
                        break
                    }
                }
            }
        }
        return isMounted
    }

2.获取U盘对应的存储路径

fun checkStorageIsMount(ctx: Context, storageType: Int = TYPE_STORAGE_USB): Boolean {
        var isMounted = false
        val sm: StorageManager = ctx.getSystemService(Context.STORAGE_SERVICE) as StorageManager
        val volumeList = SysApiInvoke.getVolumeList(sm)
        val storagePrefix: String = if (storageType == TYPE_STORAGE_SD) {
            lastSDPrefix =
                SysApiInvoke.getSysProp(SDC_PREFIX)
            lastSDPrefix
        } else {
            lastUsbPrefix =
                SysApiInvoke.getSysProp(USB1_PREFIX)
            lastUsbPrefix
        }
        for (volume in volumeList) {
            if (volume.isRemovable) {
                val usbPath: String =
                    SysApiInvoke.getVolumePath(volume)
                if (SysApiInvoke.getVolumeState(
                        sm,
                        usbPath
                    ) == Environment.MEDIA_MOUNTED) {
                    val prefix = usbPath.substring(usbPath.lastIndexOf("/") + 1)
                    if (prefix == storagePrefix) {
                        isMounted = true
                        break
                    }
                }
            }
        }
        return isMounted
    }

3.获取所有的图片文件

// file 即第二步获取到的U盘对应的存储路径
fun getAllImageFile(file: File): ArrayList<File> {
        val result = ArrayList<File>()
        if (file.isDirectory) {
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            for (listFile in file.listFiles()) {
                if (listFile.isFile) {
                    BitmapFactory.decodeFile(listFile.absolutePath, options)
                    if (options.outWidth != -1 && options.outHeight != -1) {
                        result.add(listFile)
                    }
                }
            }
        }
        return result
    }
// 判断是否包含图片
private fun isHaveImage(file: File): Boolean {
        val list = file.listFiles()
        if (list != null) {
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            for (listFile in list) {
                if (listFile.isFile) {
                    BitmapFactory.decodeFile(listFile.absolutePath, options)
                    if (options.outWidth != -1 && options.outHeight != -1) {
                        return true
                    }
                }
            }
        }
        return false
    }

系统的 API
object SysApiInvoke {

    @Suppress("JAVA_CLASS_ON_COMPANION")
    @SuppressLint("PrivateApi")
    fun setSysProp(prop: String, value: String = "") {
        try {
            val systemProperties = Class.forName("android.os.SystemProperties")
            val set = systemProperties.getMethod("set", java.lang.String::class.java, java.lang.String::class.java)
            set.invoke(systemProperties, prop, value)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @Suppress("JAVA_CLASS_ON_COMPANION")
    @SuppressLint("PrivateApi")
    fun getSysProp(prop: String, defValue: String = ""): String {
        return try {
            val systemProperties = Class.forName("android.os.SystemProperties")
            val get = systemProperties.getMethod("get", java.lang.String::class.java, java.lang.String::class.java)
            val rst = get.invoke(systemProperties, prop, defValue)
            rst as String
        } catch (e: Exception) {
            e.printStackTrace()
            defValue
        }
    }

    @Suppress("UNCHECKED_CAST")
    fun getVolumeList(sm: StorageManager): Array<StorageVolume> {
        return try {
            val getMethod = sm.javaClass.getMethod("getVolumeList")
            val rst = getMethod.invoke(sm)
            rst as Array<StorageVolume>
        } catch (e: Exception) {
            e.printStackTrace()
            emptyArray()
        }
    }

    @Suppress("UNCHECKED_CAST", "JAVA_CLASS_ON_COMPANION")
    fun getVolumeState(sm: StorageManager, path: String): String {
        return try {
            val getMethod = sm.javaClass.getMethod("getVolumeState", java.lang.String::class.java)
            val rst = getMethod.invoke(sm, path)
            rst as String
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }

    @Suppress("UNCHECKED_CAST")
    fun getVolumePath(volume: StorageVolume): String {
        return try {
            val getMethod = volume.javaClass.getMethod("getPath")
            val rst = getMethod.invoke(volume)
            rst as String
        } catch (e: Exception) {
            e.printStackTrace()
            ""
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值