3

I am using ACTION_GET_CONTENT and load selected images using Glide. I would like to display the image file size in bytes, but does Glide support a way to get this data?

I could find ways to get file size when using ACTION_GET_CONTENT, but I am afraid that if I use those methods, it may unnecessarily read the same file twice. (I read it once for the size, and Glide reads it once more to get the image). This would be particularly bad if the image is a remote image (users could select images from Google Drive).

Damn Vegetables
  • 11,484
  • 13
  • 80
  • 135
  • You are passing url to the `Glide` that means you already have `File`. Just get the File size form the `File` object . And i don't think its not unnecessarily read. File object does not read file . It only contains the properties of file not the content. – ADM Jul 28 '18 at 17:09
  • @ADM `This would be particularly bad if the image is a remote image ` – Pavneet_Singh Jul 28 '18 at 17:13
  • This is about `ACTION_GET_CONTENT` only . – ADM Jul 28 '18 at 17:15
  • Can you give me more details? The linked question has an answer like that, but it seems that `File()` takes a Java `URI`, and all I have is Android `Uri`. I have searched for ways to convert it, but it seems you cannot simply convert an Android `Uri` to a Java `URI`. – Damn Vegetables Jul 28 '18 at 17:16

1 Answers1

1

You can get the bitmap using SimpleTarget and then you can calculate the size in kb,mb by fetching bytes using getByteCount or 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);
        }
    });
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68