I'm developing application that user can post their photos, and I'm working with modify part. I get their images as File type first, store them in Firebase Storage and get download URL and store it in Firestore, and then show them with Image.network(), but in modify page I'm using Imagecropper which requires the path of image, so I have to convert it to File type, not String. Does anyone know how to get files from firebase, not downloadURL??
0
votes
3 Answers
0
votes
Use the downloadURL to download the file and save it on the device.
You can use this plugin: https://pub.dev/packages/image_downloader
If you don't want to use any plugin, have a look into this: https://stackoverflow.com/a/59356482/11847608
0
votes
If you need to download a file you have stored in cloud storage you could use writeToFile method
you can call the writeToFile method on any storage bucket reference. The location of where the file will be downloaded to is determined by the absolute path of the File instance provided,
here is the code from the Api doc
import 'package:path_provider/path_provider.dart';
Future<void> downloadFileExample() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
File downloadToFile = File('${appDocDir.path}/download-logo.png');
try {
await firebase_storage.FirebaseStorage.instance
.ref('uploads/logo.png')
.writeToFile(downloadToFile);
} on firebase_core.FirebaseException catch (e) {
// e.g, e.code == 'canceled'
}
}
0
votes
This is how I was able to solve this,
String userImageUrl = "";
File _imageFile;
uploadToStorage() async {
String imageFileName = DateTime.now().millisecondsSinceEpoch.toString();
StorageReference storageReference =
FirebaseStorage.instance.ref().child(imageFileName);
StorageUploadTask storageUploadTask = storageReference.putFile(_imageFile);
StorageTaskSnapshot taskSnapshot = await storageUploadTask.onComplete;
await taskSnapshot.ref.getDownloadURL().then((urlImage) {
userImageUrl = urlImage;
print(userImageUrl)
});
}