Reputation: 1914
if I use openFileOutput() to create and write to a temp file how do I get filesize after I'm done writing to it?
Upvotes: 105
Views: 119580
Reputation: 320
For Kotlin devs, just use this line
floor(file.length() / 1000.0 + 0.5).toInt()
The + 0.5 helps to round it to the nearest whole number.
For argument’s sake, let’s say we want to round 9.2 to the nearest integer. We can add 0.5 to it, which will give 9.7. When we use math.floor on it, which will round down, we will get 9. Now, say we want to round 9.9 to the nearest integer, we will add 0.5 to it, which will give 10.4, when we use math.floor on it, it’ll return 10
Upvotes: 1
Reputation: 56
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;
}
Upvotes: 2
Reputation: 2531
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.
Upvotes: 5
Reputation: 5241
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.
Upvotes: 244
Reputation: 47297
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"
Upvotes: 45
Reputation: 375
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..!!
Upvotes: 11