Skip to content Skip to sidebar Skip to footer

How To Download Images With Specific Size From Firebase Storage

I am using firebase storage in my android app to store images uploaded by users. all images uploaded are square in shape. I discovered that downloading this images consumes a lot o

Solution 1:

I was finally able to download images from firebase storage using MyUrlLoader class

You see, firebase storage urls look like this

firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX

As you can see above, the link already have this special question mark character ?which stands for the start of querying string so when i use CustomImageSize class, another ? was being added so the link was ending up with two ? which made downloading to fail

firebasestorage.googleapis.com/XXXX.appspot.com/Folder%2Image.png?&alt=media&token=XXX?w=200&h=200

Solution was to remove the ? in my CustomImageSize class. so it ended up like this

publicclassCustomImageSizeimplementsMyDataModel {

  privateString uri;

  publicCustomImageSize(String uri){
    this.uri = uri;
  }

  @OverridepublicStringbuildUrl(int width, int height) {

    return uri + "w=" + width + "&h=" + height;
  }
}

Although it downloaded, am not sure whether entire image was being downloaded or just the custom size one. This is because, i tried to access the image in my browser after correcting the error that was making viewing to fail, but still i was receiving an entire image. not a resized image (w=200&h=200)

Solution 2:

Try downloading your image using the following commands:-

StorageReferenceislandRef= storageRef.child("yourImage.jpg");
    // defines the specific size of your imagefinallongONE_MEGABYTE=1024 * 1024;
    islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(newOnSuccessListener<byte[]>() {
        @OverridepublicvoidonSuccess(byte[] bytes) {
            // Data for "yourImage.jpg" is returns, use this as needed
        }
    }).addOnFailureListener(newOnFailureListener() {
        @OverridepublicvoidonFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

Post a Comment for "How To Download Images With Specific Size From Firebase Storage"