I would like to upload video in Firebase Storage
.
I tried like so.
Future uploadToStorage() async {
try {
final DateTime now = DateTime.now();
final int millSeconds = now.millisecondsSinceEpoch;
final String month = now.month.toString();
final String date = now.day.toString();
final String storageId = (millSeconds.toString() + uid);
final String today = ('$month-$date');
final file = await ImagePicker.pickVideo(source: ImageSource.gallery);
StorageReference ref = FirebaseStorage.instance.ref().child("video").child(today).child(storageId);
StorageUploadTask uploadTask = ref.putFile(file);
Uri downloadUrl = (await uploadTask.future).downloadUrl;
final String url = downloadUrl.toString();
print(url);
} catch (error) {
print(error);
}
}
But the problem is that I uploaded 3 different videos. one is from real device and the others are from Ios simulator and only one video from simulator was recognized as video like this image.
File: /Users/Daibaku/Library/Developer/CoreSimulator/Devices/C99406E4-12F3-480A-82A6-F6144ADD21AB/data/Containers/Data/Application/23E82E18-9293-4EEB-AEEA-6A873F2F7CD7/tmp/image_picker_0B59CC5B-BB53-4019-BA8E-5F219374D8C8-7394-000006A2FA530CD0.MOV'
File: '/Users/Daibaku/Library/Developer/CoreSimulator/Devices/C99406E4-12F3-480A-82A6-F6144ADD21AB/data/Containers/Data/Application/23E82E18-9293-4EEB-AEEA-6A873F2F7CD7/tmp/image_picker_F9355517-8C5C-4804-9312-69E1696CAF87-7394-000006A80D46F0B7.MOV'
Those are file path from similator and bottom one was recognized as video. Does anyone know what is happening and how to fix it? Thank you!
Edit sorry actually last one on the image was uploaded manually(I put into storage from my finder). So, both simulator and real device can't upload video.
Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.
To download a file, first create a Cloud Storage reference to the file you want to download. You can create a reference by appending child paths to the root of your Cloud Storage bucket, or you can create a reference from an existing gs:// or https:// URL referencing an object in Cloud Storage.
I worked it out. The point is you have to specify the metadata content type manually like so.
Future uploadToStorage() async {
try {
final DateTime now = DateTime.now();
final int millSeconds = now.millisecondsSinceEpoch;
final String month = now.month.toString();
final String date = now.day.toString();
final String storageId = (millSeconds.toString() + uid);
final String today = ('$month-$date');
final file = await ImagePicker.pickVideo(source: ImageSource.gallery);
StorageReference ref = FirebaseStorage.instance.ref().child("video").child(today).child(storageId);
StorageUploadTask uploadTask = ref.putFile(file, StorageMetadata(contentType: 'video/mp4')); <- this content type does the trick
Uri downloadUrl = (await uploadTask.future).downloadUrl;
final String url = downloadUrl.toString();
print(url);
} catch (error) {
print(error);
}
}
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