I get an image from Firebase Storage, take a photo with the camera, or pick one from the Library. When one of these is complete, I have a class which stores an Image
so that I can use it when required.
Now I need to upload this image to Firebase Storage (modified or new, doesn't matter). Firebase allows me to use one of the following: putData
or putFile
, each needing either an Uint8List
or File
respectively.
How can I take my Image
and either get a File
or Uint8List
from it for uploading?
--
Alternatively to remedy this, how can I get a File
of my image when I retrieve it from Firebase Storage?
Either way results in providing the correct data type to upload the image, be it get a File
at the start or end.
The idea is to create a simple interface containing an image display container and a button to upload an image after selecting it from the gallery. After an image is selected, the file will be automatically uploaded to the Firebase storage and then displayed on the screen.
The Firebase SDKs for Cloud Storage add Google security to file uploads and downloads for your Firebase apps, regardless of network quality. You can use our SDKs to store images, audio, video, or other user-generated content.
When you select an image with the image picker, it returns a File, you can use await
to wait until the user selects a file then store it as a File. Here is some sample code of how you can get the file from the image picker and upload it to 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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With