Flutter- Firebase Storage上传视频

时间:2018-08-13 12:49:40

标签: firebase flutter firebase-storage

我想在Firebase Storage中上传视频。 我尝试过这样。

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

    }

但是问题是我上传了3个不同的视频。一个来自真实设备,另一个来自Ios模拟器,只有一个来自模拟器的视频被识别为类似于该图像的视频。

image here

  

文件:/ 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'

     

文件:   '/ 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-480 9312-69E1696CAF87-7394-000006A80D46F0B7.MOV'

这些是来自模拟程序的文件路径,而最下面的则被识别为视频。 有谁知道发生了什么事以及如何解决? 谢谢!

修改  抱歉,图片上的最后一个实际上是手动上传的(我从查找器中放入了存储空间)。因此,模拟器和真实设备都无法上传视频。

1 个答案:

答案 0 :(得分:0)

我解决了。关键是您必须像这样手动指定元数据内容类型。

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

}