Flutter- Firebase存储上传视频

9
我想在Firebase存储中上传视频。 我尝试了以下方法。
 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);
      }

    }

但问题在于我上传了三个不同的视频。其中一个来自真实设备,而其余两个来自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-4804-9312-69E1696CAF87-7394-000006A80D46F0B7.MOV'

这些是来自模拟器的文件路径,其中底部的被识别为视频。有谁知道发生了什么以及如何解决?谢谢!

编辑 抱歉,实际上最后一张图片是手动上传的(我从我的Finder中存储)。因此,模拟器和真实设备都无法上传视频。


1
你可以考虑使用专门的媒体 API 存储视频,而不是使用 Firebase 存储。我在这里写了一篇文章:https://www.learningsomethingnew.com/how-to-make-a-cross-platform-video-sharing-app-with-flutter-and-firebase - syonip
1个回答

8
我已经想通了。关键是你需要手动指定元数据内容类型,就像这样。
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);
  }

}

你能否将其标记为解决此问题的方案?这样每个人都知道正确的答案 :) 谢谢 - Rebar
抱歉我犯了错误,感谢您的建议 :) - Daibaku
在Flutter Web中有没有实现这个的方法? - Shashwat Aditya
我不确定。我猜Firebase存储还没有准备好用于Flutter Web。 - Daibaku
1
我们还需要手动传递元数据吗?我正在尝试这段代码,但是文件和StorageMetadata下面都显示了红线。 - GAGAN SINGH
可设置元数据 - shinriyo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接