代码之家  ›  专栏  ›  技术社区  ›  Damn Vegetables

滑动,可以获得图像文件大小(而不是尺寸)?

  •  1
  • Damn Vegetables  · 技术社区  · 6 年前

    我正在使用动作获取内容并使用glide加载选定的图像。我想以字节为单位显示图像文件大小,但是glide是否支持获取这些数据的方法?

    我能找到办法 get file size when using ACTION_GET_CONTENT 但我担心,如果使用这些方法,可能会不必要地读取同一个文件两次。(我读了一次它的大小,和glide读了它一次,以获得图像)。如果图像是远程图像(用户可以从Google Drive中选择图像),这将特别糟糕。

    1 回复  |  直到 6 年前
        1
  •  0
  •   Pavneet_Singh    6 年前

    SimpleTarget getByteCount getAllocationByteCount

    Glide
        .with(context)
        .load("https://www.somelink.com/image.png")
        .asBitmap()
        .into(new SimpleTarget<Bitmap>() {
             @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {            
                int byteCount = Integer.MIN_VALUE;
                if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT){
                     byteCount = resource.getByteCount();
                }else{
                    byteCount = resource.getAllocationByteCount();    
                }
                int sizeInKB = byteCount / 1024;
                int sizeInMB = sizeInKB / 1024; 
                image.setImageBitmap(resource);
            }
        });