Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I get file size of temp file in android?

Tags:

java

android

if I use openFileOutput() to create and write to a temp file how do I get filesize after I'm done writing to it?

like image 952
alexl Avatar asked Apr 18 '12 04:04

alexl


People also ask

How do you find the file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

Can you find the file size the number of bytes using the File class?

Using FileUtils.The class provides the sizeOf() method to get the file size in bytes. Syntax: public static long sizeOf(File file)

How do I save temp files on Android?

If you'd like to cache some data, rather than store it persistently, you should use getCacheDir() to open a File that represents the internal directory where your application should save temporary cache files. When the device is low on internal storage space, Android may delete these cache files to recover space.


2 Answers

I hope this can help you:

    File file = new File(selectedPath);     int file_size = Integer.parseInt(String.valueOf(file.length()/1024)); 

Where the String selectedPath is the path to the file whose file size you want to determine.

file.length() returns the length of the file in bytes, as described in the Java 7 Documentation:

Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist. Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.

Dividing by 1024 converts the size from bytes to kibibytes. kibibytes = 1024 bytes.

like image 126
Akash Singh Avatar answered Oct 19 '22 20:10

Akash Singh


Kotlin Extension Solution

Add these somewhere, then call myFile.sizeInMb or whichever you need

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 

If you need a File from a String or Uri, try adding these

fun Uri.asFile(): File = File(toString())  fun String?.asUri(): Uri? {     try {         return Uri.parse(this)     } catch (e: Exception) {     }     return null } 

If you'd like to easily display the values as a string, these are simple wrappers. Feel free to customize the default decimals displayed

fun File.sizeStr(): String = size.toString() fun File.sizeStrInKb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInKb) fun File.sizeStrInMb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInMb) fun File.sizeStrInGb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInGb)  fun File.sizeStrWithBytes(): String = sizeStr() + "b" fun File.sizeStrWithKb(decimals: Int = 0): String = sizeStrInKb(decimals) + "Kb" fun File.sizeStrWithMb(decimals: Int = 0): String = sizeStrInMb(decimals) + "Mb" fun File.sizeStrWithGb(decimals: Int = 0): String = sizeStrInGb(decimals) + "Gb" 
like image 20
Gibolt Avatar answered Oct 19 '22 21:10

Gibolt