java 如何在android中以KB和MB为单位获取文件大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17219802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:20:14  来源:igfitidea点击:

How to get file size in KB and MB in android

javaandroid

提问by Asca Asca

I want code that i can get file size and change it to KB and MB

我想要可以获取文件大小并将其更改为 KB 和 MB 的代码

562 KB  =  562 KB
1024 KB =    1 MB
2152 KB = 2.15 MB

thank you in advance.

先感谢您。

回答by The Badak

Here it's:

这是:

public String size(int size){
    String hrSize = "";
    double m = size/1024.0;
    DecimalFormat dec = new DecimalFormat("0.00");

    if (m > 1) {
        hrSize = dec.format(m).concat(" MB");
    } else {
        hrSize = dec.format(size).concat(" KB");
    }
    return hrSize;
}

SOURCE: Converting KB to MB, GB, TB dynamically

来源:将 KB 动态转换为 MB、GB、TB