Après avoir vérifié différentes solutions, j'ai écrit le code moi-même. Voici le code complet pour la recherche.
- Mémoire externe totale
- Mémoire externe libre
- Mémoire externe utilisée
- Mémoire interne TotaL
- Mémoire interne utilisée
- Mémoire interne libre
''''
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)
}
}
}}
1 votes
Depuis le niveau 18 de l'API, la méthode a été renommée pour se terminer par Long. Il faudrait probablement ajouter une vérification du niveau d'API précédent.
0 votes
Toutes les solutions que j'ai essayées ne fonctionnent pas, quand je fais le formatage en tant que stockage interne ... pouvez-vous m'aider, comment faire pour réaliser cela ?
0 votes
Fermé en tant que duplicate - un post plus récent a des réponses jusqu'à Android R
0 votes
Réponse parfaite ici stackoverflow.com/a/30401010/5773037