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

JavaAndroid

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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"

Solution 3 - Java

Try to use below code:

// Get file from file name
    final String dirPath = f.getAbsolutePath();
    String fileName = url.substring(url.lastIndexOf('/') + 1);
    File file = new File(dirPath + "/" + fileName);

      // Get length of file in bytes
          long fileSizeInBytes = file.length();
     // Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
          long fileSizeInKB = fileSizeInBytes / 1024;
    //  Convert the KB to MegaBytes (1 MB = 1024 KBytes)
          long fileSizeInMB = fileSizeInKB / 1024;

          if (fileSizeInMB > 27) {
          ...
          }

Hope It will work for you..!!

Solution 4 - Java

private boolean isFileLessThan2MB(File file) {
    int maxFileSize = 2 * 1024 * 1024;
    Long l = file.length();
    String fileSize = l.toString();
    int finalFileSize = Integer.parseInt(fileSize);
    return finalFileSize >= maxFileSize;
}

You can use this function as well I'm using this to check if the file size is less than 2MB when you use file.lenght funtion it returns the file size in bytes. So I've check the file size in bytes.

Solution 5 - Java

All the codes above have a slight issue because the file.length() does not return the exact size of the file. To resolve this issue, I tried multiple ways, and after a while I got the fix. Below is the code by which you can get the exact size of the file.

Note that for the bytes to KB conversion I have divided by 1000.0 instead of 1024.0. It works for me.

public static String getFolderSizeLabel(File file) {
    double size = (double) getFolderSize(file) / 1000.0; // Get size and convert bytes into KB.
    if (size >= 1024) {
        return (size / 1024) + " MB";
    } else {
        return size + " KB";
    }
}
public static long getFolderSize(File file) {
    long size = 0;
    if (file.isDirectory()) {
        for (File child : file.listFiles()) {
            size += getFolderSize(child);
        }
    } else {
        size = file.length();
    }
    return size;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalexlView Question on Stackoverflow
Solution 1 - JavaAkash SinghView Answer on Stackoverflow
Solution 2 - JavaGiboltView Answer on Stackoverflow
Solution 3 - JavaDevangiView Answer on Stackoverflow
Solution 4 - JavaApoorv PandeyView Answer on Stackoverflow
Solution 5 - JavaSreyans BoharaView Answer on Stackoverflow