How to get download url of Thumbnail after firebase resize images extension completets

Viewed 1584

The recently introduced firebase extension 'Image resize' produces a thumbnail once a picture is uploaded to a storage bucket.

How do I obtain the download url of the image of this thumbnail, after the extension completes?

final StorageReference storageRef =
        FirebaseStorage.instance.ref().child(fileName);

    final StorageUploadTask uploadTask = storageRef.putFile(
      File(path),
    );

    final StorageTaskSnapshot downloadUrl = (await uploadTask.onComplete);

    final String url = (await downloadUrl.ref.getDownloadURL()); //This will give me the download url of file before resize

// ??How do I the download url of resized image that gets stored in fileName/thumbnails folder

1 Answers

When you upload an image file to your specified Cloud Storage bucket, this extension:

  1. Creates a resized image with your specified dimensions.
  2. Stores the resized image in the same Storage bucket as the original uploaded image.
  3. Names the resized image using the same name as the original uploaded image, but suffixed with your specified width and height.

For example, if you specify a path here of thumbs and you upload an image to /images/original.jpg, then the resized image is stored at /images/thumbs/original_200x200.jpg

So your file's url will be-

 String name = url.substring(url.lastIndexOf("/")+1,url.indexOf("."));
 String urlStr = "thumbnails/"+name+"_"+width+"x"+height+url.substring(url.indexOf("."),url.length());
 storageRef.child(url.replace(name,urlStr)).getDownloadUrl()
     .addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
           // Got the download URL for 'users/me/profile.png'
     }})  
Related