image - Flutter - 将图像上传到 Firebase 存储

我从 Firebase 存储中获取图像,用相机拍照,或者从库中挑选一张。当其中一个完成时,我有一个存储 Image 的类,以便我可以在需要时使用它。

现在我需要将此图片上传到 Firebase 存储(修改或新的,没关系)。 Firebase 允许我使用以下之一:putDataputFile,每个分别需要 Uint8ListFile .

如何获取我的 Image 并从中获取 FileUint8List 进行上传?

--

另外,为了解决这个问题,当我从 Firebase 存储中检索图像时,如何获取图像的文件

无论是哪种方式,都可以提供正确的数据类型来上传图片,无论是在开始还是结束时获取 File

最佳答案

当你使用图像选择器选择图像时,它会返回一个文件,你可以使用 await 等待用户选择一个文件,然后将其存储为文件。下面是一些示例代码,说明如何从图像选择器获取文件并将其上传到 Firebase。

    FirebaseStorage _storage = FirebaseStorage.instance;

    Future<Uri> uploadPic() async {

    //Get the file from the image picker and store it 
    File image = await ImagePicker.pickImage(source: ImageSource.gallery);  

    //Create a reference to the location you want to upload to in firebase  
    StorageReference reference = _storage.ref().child("images/");

    //Upload the file to firebase 
    StorageUploadTask uploadTask = reference.putFile(file);

    // Waits till the file is uploaded then stores the download url 
    Uri location = (await uploadTask.future).downloadUrl;

    //returns the download url 
    return location;
   }

https://stackoverflow.com/questions/51857796/

相关文章:

flutter - 在创建带有 float 应用栏的应用时使用 StreamBuilder

flutter - 如何在 Flutter 应用中获取屏幕大小?

flutter - 如何在 flutter 中更改 textField 的下划线颜色?

dart - Flutter 嵌套小部件回调/异步问题

android - Flutter Android Studio 中缺少设备文件资源管理器选项

flutter - 如何知道小部件在视口(viewport)中是否可见?

multithreading - 如何在 Flutter 中在后台运行任务?

dart - 在 flutter 中使用其 GlobalKey 获取 Widget 的高度

flutter - 在 Flutter 的嵌套 Navigator 结构中,如何获取特定的 Navi

dart - 如何将 showModalBottomSheet 设置为全高?