作者:EvilMaknaeKYU | 来源:互联网 | 2022-12-07 16:57
我从Firebase Storage获取图像,用相机拍摄照片,或从媒体库中选择一个。当其中一个完成时,我有一个存储一个的类,Image
以便在需要时可以使用它。
现在,我需要将此图像上传到Firebase存储(已修改或新的都无所谓)。Firebase允许我使用以下之一:putData
或putFile
,每个需要分别为Uint8List
或File
。
我怎样才能拿到我的,Image
然后从中获取一个File
或Uint8List
从中上传?
-
或者,要解决此问题,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;
}