Glide,可以获取图像文件大小(不是尺寸)吗?

Glide, possible to get image file size (not dimensions)?

我正在使用 ACTION_GET_CONTENT 并使用 Glide 加载 selected 图像。我想以字节为单位显示图像文件大小,但 Glide 是否支持获取此数据的方法?

我可以找到 get file size when using ACTION_GET_CONTENT 的方法,但我担心如果我使用这些方法,它可能会不必要地读取同一个文件两次。 (我为了大小读了一遍,Glide 又读了一遍以获得图像)。如果图像是远程图像(用户可以 select 来自 Google 驱动器的图像),这将特别糟糕。

您可以使用 SimpleTarget 获取位图,然后您可以使用 getByteCount or getAllocationByteCount

获取字节来计算以 kb、mb 为单位的大小
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);
        }
    });