How to download images with specific size from firebase storage

前端 未结 2 1803
萌比男神i
萌比男神i 2021-01-14 02:39

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

相关标签:
2条回答
  • 2021-01-14 03:19

    Try downloading your image using the following commands:-

    StorageReference islandRef = storageRef.child("yourImage.jpg");
        // defines the specific size of your image
        final long ONE_MEGABYTE = 1024 * 1024;
        islandRef.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
            @Override
            public void onSuccess(byte[] bytes) {
                // Data for "yourImage.jpg" is returns, use this as needed
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle any errors
            }
        });
    
    0 讨论(0)
  • 2021-01-14 03:23

    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

        public class CustomImageSize implements MyDataModel {
    
      private String uri;
    
      public CustomImageSize(String uri){
        this.uri = uri;
      }
    
      @Override
      public String buildUrl(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)

    0 讨论(0)
提交回复
热议问题