我从Firebase Storage获取图像,用相机拍照,或从库中挑选一个。当其中一个完成时,我有一个存储Image
的类,这样我就可以在需要时使用它。
现在,我需要将此图像上传到Firebase Storage (修改或更新,都无关紧要)。Firebase允许我使用以下之一:putData
或putFile
,每个都需要一个Uint8List
或File
。
我怎么才能拿着我的Image
并从中获取File
或Uint8List
进行上传呢?
--
或者,为了解决这个问题,当我从Firebase存储中检索我的图像时,如何获取它的File
?
无论哪种方式,都会导致提供正确的数据类型来上传图像,无论是在开始还是结束时获取一个File
。
发布于 2018-08-15 13:52:08
当您使用图像拾取器选择一个图像时,它会返回一个文件,您可以使用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;
}
发布于 2020-04-24 14:14:29
选择图像(画廊、相机..等)首先你可以使用图像拾取器package
然后像这样获取文件格式的图像
//Get the file from the image picker and store it
final pickedFile = await picker.getImage(source: ImageSource.camera);
File image;
if (pickedFile != null) {
image = File(pickedFile.path);
} else {
print('No image selected.');
return;
}
然后检测您要保存此文件图像的引用
FirebaseStorage storage = FirebaseStorage.instance;
//Create a reference to the location you want to upload to in firebase
StorageReference reference = storage.ref().child("profileImages/myImageUid");
最后开始上传并等待完成,然后获取URL图像
//Upload the file to firebase
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();
完整代码
FirebaseStorage storage = FirebaseStorage.instance;
File image;
try {
//Get the file from the image picker and store it
image = await ImagePicker.pickImage(source: ImageSource.gallery);
} on PlatformException catch (e) {
//PlatformException is thrown with code : this happen when user back with don't
//selected image or not approve permission so stop method here
// check e.code to know what type is happen
return;
}
//Create a reference to the location you want to upload to in firebase
StorageReference reference =
storage.ref().child("profileImages/${user.id}");
//Upload the file to firebase
StorageUploadTask uploadTask = reference.putFile(image);
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
// Waits till the file is uploaded then stores the download url
String url = await taskSnapshot.ref.getDownloadURL();
发布于 2020-04-17 22:06:58
很抱歉我的回答太晚了,但上面的答案有点陈旧,希望我的答案能对你有所帮助。下面的代码示例将在以下依赖项上工作。
关于这个问题,它说我如何在上传后检索它。您可以使用下载URL来获取文件或图像,您可以下载它,也可以在NetworkImage
小部件上显示它,以便向用户显示它。我已经在下面展示了如何获取下载URL。
pubspec.yaml
dependencies
image_picker: ^0.6.5
firebase_storage: ^3.1.5
Dart代码
//Creating a global Variable
StorageReference storageReference = FirebaseStorage.instance.ref();
File _image;
void getImage(){
_image = await ImagePicker.pickImage(source: ImageSource.gallery);
}
void addImageToFirebase(){
//CreateRefernce to path.
StorageReference ref = storageReference.child("yourstorageLocation/");
//StorageUpload task is used to put the data you want in storage
//Make sure to get the image first before calling this method otherwise _image will be null.
StorageUploadTask storageUploadTask = ref.child("image1.jpg").putFile(_image);
if (storageUploadTask.isSuccessful || storageUploadTask.isComplete) {
final String url = await ref.getDownloadURL();
print("The download URL is " + url);
} else if (storageUploadTask.isInProgress) {
storageUploadTask.events.listen((event) {
double percentage = 100 *(event.snapshot.bytesTransferred.toDouble()
/ event.snapshot.totalByteCount.toDouble());
print("THe percentage " + percentage.toString());
});
StorageTaskSnapshot storageTaskSnapshot =await storageUploadTask.onComplete;
downloadUrl1 = await storageTaskSnapshot.ref.getDownloadURL();
//Here you can get the download URL when the task has been completed.
print("Download URL " + downloadUrl1.toString());
} else{
//Catch any cases here that might come up like canceled, interrupted
}
}
https://stackoverflow.com/questions/51857796
复制相似问题