热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Flutter-将图像上传到Firebase存储

如何解决《Flutter-将图像上传到Firebase存储》经验,为你挑选了1个好方法。

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

现在,我需要将此图像上传到Firebase存储(已修改或新的都无所谓)。Firebase允许我使用以下之一:putDataputFile,每个需要分别为Uint8ListFile

我怎样才能拿到我的,Image然后从中获取一个FileUint8List从中上传?

-

或者,要解决此问题,File当我从Firebase Storage检索图像时如何获得图像?

无论哪种方式,都会提供正确的数据类型来上传图像,无论图像是File在开始还是结束时都显示为。



1> Nash..:

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

    FirebaseStorage _storage = FirebaseStorage.instance;

    Future 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;
   }


推荐阅读
author-avatar
EvilMaknaeKYU
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有