格斗家国夫君 2019-09-05 11:41 采纳率: 25%
浏览 343
已结题

Android 8.0 计算文件大小,转换是不是有问题

打印这句 Log.d("zll","481441=="+Formatter.formatFileSize(this,481441));

结果是这样的:D/zll: 481441==481 kB

网页在线转换的结果是: 470.1572265625 KB

是不是有问题啊,不应该除以1024吗,求大佬释疑

  • 写回答

1条回答 默认 最新

  • moonFY 2019-09-05 16:01
    关注
        public static String formatFileSize(@Nullable Context context, long sizeBytes) {
            if (context == null) {
                return "";
            }
            final BytesResult res = formatBytes(context.getResources(), sizeBytes, FLAG_SI_UNITS);
            return bidiWrap(context, context.getString(com.android.internal.R.string.fileSizeSuffix,
                    res.value, res.units));
        }
    
            public static BytesResult formatBytes(Resources res, long sizeBytes, int flags) {
            final int unit = ((flags & FLAG_IEC_UNITS) != 0) ? 1024 : 1000;
                    ...
            }
    
          public static final int FLAG_SI_UNITS = 1 << 2;
    
        public static final int FLAG_IEC_UNITS = 1 << 3;
    
            因此unit = 1000;
            所以 是 481 不是 470
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示