我正在使用ACTION_GET_CONTENT,并使用Glide加载选定的图像。我想以字节为单位显示图像文件的大小,但是Glide是否支持获取此数据的方法?
我可以找到get file size when using ACTION_GET_CONTENT的方法,但是如果使用这些方法,恐怕它可能不必要地两次读取同一文件。 (我只读取了一次以获取尺寸,而Glide再次读取了以获取图像)。如果该图像是远程图像(用户可以从Google云端硬盘中选择图像),则情况尤其糟糕。
答案 0 :(得分:0)
您可以使用SimpleTarget
获取位图,然后可以使用getByteCount或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);
}
});