如何在Firebase调整图片大小扩展完成后获取缩略图的下载网址



最近推出的Firebase扩展程序'图片调整大小'会在图片上传到存储空间后生成缩略图。

扩展完成后,如何获取此缩略图图像的下载网址?

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

当您将图像文件上传到指定的Cloud Storage bucket时,此扩展名:

  1. 使用指定的尺寸创建调整大小的图像。
  2. 将调整大小的图像存储在与原始上传图像相同的存储桶中。
  3. 使用与原始上传图像相同的名称来命名调整后的图像,但后缀为指定的宽度和高度。

例如,如果您在此处指定拇指路径并将图像上传到/images/original.jpg,则调整大小的图像存储在/images/thumbs/original_200x200.jpg

所以你文件的网址将是——

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'
}})  

最新更新