Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter- Firebase Storage upload video

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.

image here

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.

like image 768
Daibaku Avatar asked Aug 13 '18 12:08

Daibaku


People also ask

Can we upload video to Firebase storage?

Cloud Storage for Firebase allows you to quickly and easily upload files to a Cloud Storage bucket provided and managed by Firebase.

How do I get pictures from Firebase storage flutter?

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.


1 Answers

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);
  }

}
like image 194
Daibaku Avatar answered Sep 19 '22 02:09

Daibaku