Home > Software design >  How to find the size of files in kotlin - Android studio
How to find the size of files in kotlin - Android studio

Time:05-19

I was trying to find the size of the file in kotlin, using path.file size . It is crashing with file not found exception.

file.getname is working find and is giving me the file name.

any leads would be appreciated.

CodePudding user response:

Us these set of extension functions to get the size.

val File.size get() = if (!exists()) 0.0 else length().toDouble()
val File.sizeInKb get() = size / 1024
val File.sizeInMb get() = sizeInKb / 1024
val File.sizeInGb get() = sizeInMb / 1024
val File.sizeInTb get() = sizeInGb / 1024

Ref: SO Question

CodePudding user response:

There are different ways to get the file size you can implement your own extension method and other way is

 Formatter.formatShortFileSize(context, File(absolutePath).length())
  • Related