嗨,我有一个文件列表。我想显示每个文件的文件长度。我创建自己的方法来获取文件长度和正确的单位(基值)。
来源
public String getFileLength(long lengthBytes){
Log.i("", 1 + " -- FILE INFO -- " + lengthBytes);
String lengthString;
if(lengthBytes<1024){
Log.i("", 21 + " -- FILE INFO B -- " + lengthBytes);
lengthString = lengthBytes + " B";
Log.i("", 22 + " -- FILE INFO B -- " + lengthString);
} else if (lengthBytes<(1024^2)){
Log.i("", 31 + " -- FILE INFO KB -- " + lengthBytes);
//length = String.valueOf(Math.round(i/1024)) + " KB";
lengthString = lengthBytes/1024 + " KB";
Log.i("", 32 + " -- FILE INFO KB -- " + lengthString);
} else if (lengthBytes<(1024^3)){
Log.i("", 41 + " -- FILE INFO MB -- " + lengthBytes);
lengthString = lengthBytes/(1024^2) + " MB";
Log.i("", 42 + " -- FILE INFO MB -- " + lengthBytes);
} else if (lengthBytes<(1024^4)){
Log.i("", 51 + " -- FILE INFO GB -- " + lengthBytes);
lengthString = lengthBytes / (1024 ^ 3) + " GB";
Log.i("", 52 + " -- FILE INFO GB -- " + lengthBytes);
} else lengthString = "ERR";
return lengthString;
}
它在应用程序ERR中显示了我。我想调试它,所以我添加了很多Log.i. logcat告诉我你可以在这句话下看到。
Logcat向我展示了这个:
06-30 11:50:23.523 1590-1590/info.myapplication15.app I/﹕ 1 -- FILE INFO -- 292303
06-30 11:50:24.253 1590-1590/info.myapplication15.app D/dalvikvm﹕ GC_FOR_ALLOC freed 99K, 5% free 3268K/3436K, paused 58ms, total 65ms
06-30 11:50:24.263 1590-1590/info.myapplication15.app I/dalvikvm-heap﹕ Grow heap (frag case) to 4.330MB for 1127536-byte allocation
06-30 11:50:24.443 1590-1599/info.myapplication15.app D/dalvikvm﹕ GC_FOR_ALLOC freed 6K, 4% free 4362K/4540K, paused 95ms, total 95ms
06-30 11:50:25.503 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070040}
06-30 11:50:25.563 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070041}
06-30 11:50:25.683 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070042}
06-30 11:50:26.103 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070040}
06-30 11:50:26.143 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070041}
06-30 11:50:26.223 1590-1590/info.myapplication15.app W/Resources﹕ Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f070042}
06-30 11:50:26.753 1590-1590/info.myapplication15.app I/Choreographer﹕ Skipped 180 frames! The application may be doing too much work on its main thread.
我想用圆号和单位显示字符串。
我读到我应该使用Math.round(i/1024)
我希望(文件长度为292303)显示:285.45 KB
答案 0 :(得分:1)
只需使用以下代码:
public String round(long unrounded) {
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(1, BigDecimal.ROUND_CEILING);
return String.valueOf(rounded.longValue()) ;
}
或者只使用Math.ceil()代替round()
..
编辑:只需使用我在下面提供的this link中接受的答案。
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}