Uploading network image to firestore - flutter

How can I download an image file from network using a given url and upload it to fire store?
I am having hard time downloading image from url and uploading it to fire store.

you could do it for example like this:
upload() async {
//pick image use ImageSource.camera for accessing camera.
File image = await ImagePicker.pickImage(source: ImageSource.gallery);
//alternative with ressource in your app:
var src = // your image src
File image = await Image.network(src);
//basename() function will give you the filename
String fileName = basename(image.path);
//passing your path with the filename to Firebase Storage Reference
StorageReference reference =
FirebaseHelper.firebaseStorage().child("your_path/$fileName");
//upload the file to Firebase Storage
StorageUploadTask uploadTask = reference.putFile(image);
//Snapshot of the uploading task
StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
}
To get a better understanding of firebase storage and the upload process, and features like showing progress and more I recommend you checking this useful article too: https://medium.com/flutterpub/firebase-cloud-storage-and-flutter-fa2e91663b95

Related

upload and get image from firebase storage flutter web

I am currently developing a web filter application
In the application, when I enter product details, I need to choose an image of the product
But I face a problem, when I choose an image from the computer, I cannot get the image path, the name and the bytes only, and when I upload the bytes to the firebase, I cannot get the image because it is bytes. How can I calculate the path of the image and display it in a list?I am currently developing a flutter web application
In the application, when I enter product details, I need to choose an image for the product from my PC
But I face a problem, when I choose an image from the computer, I cannot get the image path, the (i get name and the bytes only), and when I upload the bytes to the firebase, I cannot get the image because it is bytes not as path. How can I get the path of the image and display it in a list products?
Uint8List? fileBytes;
String? fileName;
String? picUrl;
void chooseImage() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
fileBytes = result.files.first.bytes;
fileName = result.files.first.name;
}
}
uploadImageToFirebase() async {
UploadTask _uploadTask = (await FirebaseStorage.instance
.ref('uploads/$fileName')
.putData(fileBytes!)) as UploadTask;
picUrl = await (await _uploadTask).ref.getDownloadURL();
}

Uploading audio to firebase flutter times out

I am trying to upload an audio file to firebase within my flutter app.
The user flow is filePicker -> confirm screen (where the user picks the details of the song like name, caption, etc). If I wait too long in the confirm screen (longer than the length of the audio file), the file "times out" and returns false for file.existsSync().
However, whenever I run through the confirm screen super fast, it works because I uploaded it before the length of the audio file finishes. I assume this has to do with caching, but I have no idea how to fix this.
My code for uploading is below:
Future<String> _uploadSongToStorage(String id, String songPath) async {
Reference ref = firebaseStorage.ref().child('songs').child(id);
//assert(songFile.existsSync());
UploadTask uploadTask = ref.putFile(File(songPath));
TaskSnapshot snap = await uploadTask;
String downloadUrl = await snap.ref.getDownloadURL();
return downloadUrl;}

How do I get a public link for my video stored in firebase

I am using a service called mux in which it takes a url link to a video and encodes transcodes and decodes for you. It takes a link to a video. However how do I get this linke in firebase flutter?
Current This is what I am doing. This leads to the video only showing the first frame.
await firebase_storage.FirebaseStorage.instance
.ref().child('Users/$currentSignedInEmail/Video/${titleController.text}')
.putFile(compressedVersion).catchError((error){
});
//TODO: need to fix this does not load properly
_muxClient.storeVideo(videoUrl: await firebase_storage.FirebaseStorage.instance
.ref().child('Users/$currentSignedInEmail/Video/${titleController.text}').getDownloadURL());
Try this:
Reference reference = firebase_storage.FirebaseStorage.instance
.ref()
.child('Users/$currentSignedInEmail/Video/${titleController.text}');
UploadTask task = reference.putFile(compressedVersion);
await task;
var url = await reference.getDownloadURL();
_muxClient.storeVideo(url);

How to Share Image from API(URL) in flutter?

Here I want to share image which I get through API. I tried different method for this functionality but I did not get any solution because every solutions have for only one image.
Exactly, I get multiple image from Url and I open any particular image in next page. So, I want to share that image which I opened in another page.
I tried for sharing image but I could not did this. Whenever I try to share that image, Image url share with sharing option on device but I want share image not URl of image How I can accomplish this?
You have to download the image like this:
http.Response response = await http.get(url);
then create an image on the device using the downloaded image like this (Read and Write Files):
final directory = await getTemporaryDirectory();
final path = directory.path;
final file = File('$path/image.png');
file.writeAsBytes(response.bodyBytes);
The getTemporaryDirectory() is in the plugin path_provider. Now, you have the image you'd like to share stored in the temporarily as "image.png" and you can use the share_plus plugin to share the image like this:
Share.shareFiles(['$path/image.png']);
Thanks to #abdulrazak, At first, you have to download the image in a temporary path & then you can share the image as you want. used Dio to download the image & share_plus for sharing
void _shareNetworkImage(String url) async {
Directory tempDir = await getTemporaryDirectory();
final path = '${tempDir.path}/test.jpeg';
await Dio().download(url, path);
Share.shareFiles([path]);
}
You can use the esys_flutter_share plugin.
Install it, and then get dependencies:
dependencies:
esys_flutter_share: ^1.0.2
Import the plugin to your code:
import 'package:esys_flutter_share/esys_flutter_share.dart';
Use the below code:
var request = await HttpClient().getUrl(Uri.parse('https://yourImageURL.jpg'));
var response = await request.close();
Uint8List bytes = await consolidateHttpClientResponseBytes(response);
await Share.file('ESYS AMLOG', 'amlog.jpg', bytes, 'image/jpg');

Flutter update image in storage

I am using ImagePickerWeb and upload an images in Storage. For display this images I am using url. If user change image, how to update in storage? The first need delete previous image and upload again?
Future uploadImg() async {
var timekey = DateTime.now();
fb.StorageReference storageReference =
fb.storage().ref('imgProduct/${timekey.toString()}.jpg');
fb.UploadTaskSnapshot uploadTask = await storageReference
.put(_image1, fb.UploadMetadata(contentType: 'image/jpg'))
.future;
var imageUrl = await uploadTask.ref.getDownloadURL();
url = imageUrl.toString();
print('Image Url' + url);
}
you need to update the database with the new photo url and then delete the old image from firebase storage.