|
|
|
@ -1,13 +1,12 @@
|
|
|
|
|
package com.common.commonlib.utils
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.os.StatFs
|
|
|
|
|
import android.os.storage.StorageManager
|
|
|
|
|
import android.os.storage.StorageVolume
|
|
|
|
|
import java.lang.reflect.InvocationTargetException
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
val USB_KEYWORDS = arrayOf("USB", "U 盘")
|
|
|
|
|
@JvmField
|
|
|
|
|
val NVME_KEYWORDS = arrayOf("新加卷")
|
|
|
|
|
|
|
|
|
|
object StorageUtils {
|
|
|
|
@ -17,7 +16,7 @@ object StorageUtils {
|
|
|
|
|
* @param keyword SD = "内部存储"; EXT = "SD卡"; USB = "U盘"
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
fun getStoragePath(mContext: Context, keywords: Array<String> = USB_KEYWORDS): String? {
|
|
|
|
|
fun getStoragePath(mContext: Context, isUsb: Boolean = true): String? {
|
|
|
|
|
var targetpath: String? = ""
|
|
|
|
|
val mStorageManager = mContext
|
|
|
|
|
.getSystemService(Context.STORAGE_SERVICE) as StorageManager
|
|
|
|
@ -34,9 +33,17 @@ object StorageUtils {
|
|
|
|
|
val storageVolumeElement: StorageVolume = result[i]
|
|
|
|
|
val userLabel = getUserLabel.invoke(storageVolumeElement) as String
|
|
|
|
|
val path = getPath.invoke(storageVolumeElement) as String
|
|
|
|
|
if (keywords.contains(userLabel))
|
|
|
|
|
if (isUsb) {
|
|
|
|
|
if (USB_KEYWORDS.contains(userLabel)) {
|
|
|
|
|
targetpath = path
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 大于400GB的也当硬盘看待
|
|
|
|
|
if (NVME_KEYWORDS.contains(userLabel) || getSizeInGB(path) > 400) {
|
|
|
|
|
targetpath = path
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (e: ClassNotFoundException) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} catch (e: InvocationTargetException) {
|
|
|
|
@ -48,4 +55,21 @@ object StorageUtils {
|
|
|
|
|
}
|
|
|
|
|
return targetpath
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const val BYTE_GB = 1024 * 1024 * 1024f
|
|
|
|
|
private fun getSizeInGB(path: String): Int {
|
|
|
|
|
val statfs = StatFs(path)
|
|
|
|
|
val blocSize = statfs.blockSizeLong
|
|
|
|
|
val totalSize = statfs.blockCountLong * blocSize
|
|
|
|
|
val gb = (totalSize / BYTE_GB).toInt()
|
|
|
|
|
return gb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun getAvailableSizeSizeInGB(path: String): Float {
|
|
|
|
|
val statfs = StatFs(path)
|
|
|
|
|
val blocSize = statfs.blockSizeLong
|
|
|
|
|
val availableSize = statfs.availableBlocksLong * blocSize
|
|
|
|
|
val gb = (availableSize / BYTE_GB)
|
|
|
|
|
return gb
|
|
|
|
|
}
|
|
|
|
|
}
|