简体   繁体   English

如何在android中获取临时文件的文件大小?

[英]how do I get file size of temp file in android?

如果我使用 openFileOutput() 创建并写入一个临时文件,在我完成写入后如何获得文件大小?

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.其中 String selectedPath是要确定其文件大小的文件的路径。

file.length() returns the length of the file in bytes, as described in the Java 7 Documentation : file.length()以字节为单位返回文件的长度,如Java 7 文档中所述

Returns the length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist.返回此抽象路径名表示的文件的长度(以字节为单位),如果文件不存在,则返回 0L。 Some operating systems may return 0L for pathnames denoting system-dependent entities such as devices or pipes.某些操作系统可能会为表示系统相关实体(例如设备或管道)的路径名返回 0L。

Dividing by 1024 converts the size from bytes to kibibytes.除以 1024 会将大小从字节转换为千比字节。 kibibytes = 1024 bytes.千比字节 = 1024 字节。

Kotlin Extension Solution Kotlin 扩展解决方案

Add these somewhere, then call myFile.sizeInMb or whichever you need在某处添加这些,然后调用myFile.sizeInMb或任何你需要的

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如果您需要来自字符串或 Uri 的文件,请尝试添加这些

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"

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..!!希望它对你有用..!!

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.您也可以使用此函数,我使用它来检查文件大小是否小于 2MB,当您使用 file.lenght 函数时,它以字节为单位返回文件大小。 So I've check the file size in bytes.所以我检查了文件大小(以字节为单位)。

All the codes above have a slight issue because the file.length() does not return the exact size of the file .上面的所有代码都有一个小问题,因为file.length() 没有返回文件的确切大小 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.请注意,对于字节到 KB 的转换,我除以 1000.0而不是 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM