Last active
January 14, 2022 02:27
-
-
Save labibmuhajir/d7de0c095aee952d9a5904ed4a527221 to your computer and use it in GitHub Desktop.
storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val path = ContextCompat.getExternalFilesDirs(requireContext(), null).first() | |
val bytesAvailable = path.freeSpace | |
val bytesSize = path.totalSpace | |
fun formatSize(size: Long): String? { | |
var size = size | |
var suffix: String? = null | |
if (size >= 1024) { | |
suffix = " KB" | |
size /= 1024 | |
if (size >= 1024) { | |
suffix = " MB" | |
size /= 1024 | |
} | |
} | |
val resultBuilder = StringBuilder(java.lang.Long.toString(size)) | |
var commaOffset = resultBuilder.length - 3 | |
while (commaOffset > 0) { | |
resultBuilder.insert(commaOffset, ',') | |
commaOffset -= 3 | |
} | |
if (suffix != null) resultBuilder.append(suffix) | |
return resultBuilder.toString() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment