【问题标题】:Glide, possible to get image file size (not dimensions)?Glide,可以获得图像文件大小(不是尺寸)吗?
【发布时间】:2018-07-28 16:52:24
【问题描述】:

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

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

【问题讨论】:

  • 您将 url 传递给 Glide,这意味着您已经拥有 File。只需从 File 对象中获取文件大小。而且我不认为它没有必要阅读。文件对象不读取文件。它只包含文件的属性而不是内容。
  • @ADM This would be particularly bad if the image is a remote image
  • 这只是关于ACTION_GET_CONTENT。
  • 你能给我更多的细节吗?链接的问题有这样的答案,但似乎File() 采用Java URI,而我所拥有的只是Android Uri。我已经搜索了转换它的方法,但似乎您不能简单地将 Android Uri 转换为 Java URI。

标签: android android-glide


【解决方案1】:

您可以使用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);
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 2017-04-07
    相关资源
    最近更新 更多