在检查了不同的解决方案之后,我自己编写了代码,这是查找的完整代码
- 外部记忆体总数
- 可用的外部存储器
- 使用的外部存储器
- 内置内存
- 使用的内部存储器
- 可用内部内存
''''
object DeviceMemoryUtil {
private const val error: String = "Something went wrog"
private const val noExternalMemoryDetected = "No external Storage detected"
private var totalExternalMemory: Long = 0
private var freeExternalMemory: Long = 0
private var totalInternalStorage: Long = 0
private var freeInternalStorage: Long = 0
/**
* Checks weather external memory is available or not
*/
private fun externalMemoryAvailable(): Boolean {
return Environment.getExternalStorageState() ==
Environment.MEDIA_MOUNTED
}
/**
*Gives total external memory
* @return String Size of external memory
* @return Boolean True if memory size is returned
*/
fun getTotalExternalMemorySize(): Pair<String?, Boolean> {
val dirs: Array<File> = ContextCompat.getExternalFilesDirs(CanonApplication.getCanonAppInstance(), null)
return if (externalMemoryAvailable()) {
if (dirs.size > 1) {
val stat = StatFs(dirs[1].path)
val blockSize = stat.blockSizeLong
val totalBlocks = stat.blockCountLong
var totalExternalSize = totalBlocks * blockSize
totalExternalMemory = totalExternalSize
Pair(formatSize(totalExternalSize), true)
} else {
Pair(error, false)
}
} else {
Pair(noExternalMemoryDetected, false)
}
}
/**
* Gives free external memory size
* @return String Size of free external memory
* @return Boolean True if memory size is returned
*/
fun getAvailableExternalMemorySize(): Pair<String?, Boolean> {
val dirs: Array<File> = ContextCompat.getExternalFilesDirs(CanonApplication.getCanonAppInstance(), null)
if (externalMemoryAvailable()) {
return if (dirs.size > 1) {
val stat = StatFs(dirs[1].path)
val blockSize = stat.blockSizeLong
val availableBlocks = stat.availableBlocksLong
var freeExternalSize = blockSize * availableBlocks
freeExternalMemory = freeExternalSize
Pair(formatSize(freeExternalSize), true)
} else {
Pair(error, false)
}
} else {
return Pair(noExternalMemoryDetected, false)
}
}
/**
* Gives used external memory size
* @return String Size of used external memory
* @return Boolean True if memory size is returned
*/
fun getUsedExternalMemorySize(): Pair<String?, Boolean> {
return if (externalMemoryAvailable()) {
val totalExternalSize = getTotalExternalMemorySize()
val freeExternalSize = getAvailableExternalMemorySize()
if (totalExternalSize.second && freeExternalSize.second) {
var usedExternalVolume = totalExternalMemory - freeExternalMemory
Pair(formatSize(usedExternalVolume), true)
} else {
Pair(error, false)
}
} else {
Pair(noExternalMemoryDetected, false)
}
}
/**
*Formats the long to size of memory in gb,mb etc.
* @param size Size of memory
*/
fun formatSize(size: Long): String? {
return android.text.format.Formatter.formatFileSize(CanonApplication.getCanonAppInstance(), size)
}
/**
* Gives total internal memory size
* @return String Size of total internal memory
* @return Boolean True if memory size is returned
*/
fun getTotalInternalStorage(): Pair<String?, Boolean> {
if (showStorageVolumes()) {
return Pair(formatSize(totalInternalStorage), true)
} else {
return Pair(error, false)
}
}
/**
* Gives free or available internal memory size
* @return String Size of free internal memory
* @return Boolean True if memory size is returned
*/
fun getFreeInternalStorageVolume(): Pair<String?, Boolean> {
return if (showStorageVolumes()) {
Pair(formatSize(freeInternalStorage), true)
} else {
Pair(error, false)
}
}
/**
*For calculation of internal storage
*/
private fun showStorageVolumes(): Boolean {
val storageManager = CanonApplication.canonApplicationInstance.applicationContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager
val storageStatsManager = CanonApplication.canonApplicationInstance.applicationContext.getSystemService(Context.STORAGE_STATS_SERVICE) as StorageStatsManager
if (storageManager == null || storageStatsManager == null) {
return false
}
val storageVolumes: List<StorageVolume> = storageManager.storageVolumes
for (storageVolume in storageVolumes) {
var uuidStr: String? = null
storageVolume.uuid?.let {
uuidStr = it
}
val uuid: UUID = if (uuidStr == null) StorageManager.UUID_DEFAULT else UUID.fromString(uuidStr)
return try {
freeInternalStorage = storageStatsManager.getFreeBytes(uuid)
totalInternalStorage = storageStatsManager.getTotalBytes(uuid)
true
} catch (e: Exception) {
// IGNORED
false
}
}
return false
}
fun getTotalInternalExternalMemory(): Pair<Long?, Boolean> {
if (externalMemoryAvailable()) {
if (getTotalExternalMemorySize().second) {
if (getTotalInternalStorage().second) {
return Pair(totalExternalMemory + totalInternalStorage, true)
} else {
return Pair(0, false)
}
}
return Pair(0, false)
} else {
if (getTotalInternalStorage().second) {
return Pair(totalInternalStorage, true)
} else {
return Pair(0, false)
}
}
}
fun getTotalFreeStorage(): Pair<Long,Boolean> {
if (externalMemoryAvailable()){
if(getFreeInternalStorageVolume().second){
getFreeInternalStorageVolume()
getAvailableExternalMemorySize()
return Pair(freeExternalMemory + freeInternalStorage,true)
}
else{
return Pair(0,false)
}
}
else {
if (getFreeInternalStorageVolume().second){
getFreeInternalStorageVolume()
return Pair(freeInternalStorage,true)
}
else{
return Pair(0,false)
}
}
}}