Skip to content

Instantly share code, notes, and snippets.

@labibmuhajir
Last active January 14, 2022 02:27
Show Gist options
  • Save labibmuhajir/d7de0c095aee952d9a5904ed4a527221 to your computer and use it in GitHub Desktop.
Save labibmuhajir/d7de0c095aee952d9a5904ed4a527221 to your computer and use it in GitHub Desktop.
storage
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