需要开通系统权限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()
""
}
}
}