Flutter- Firebase Storage上传视频 [英] Flutter- Firebase Storage upload video
问题描述
我想在Firebase Storage
中上传视频.
我尝试过这样.
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);
}
}
但是问题是我上传了3个不同的视频.一个来自真实设备,另一个来自Ios模拟器,只有一个来自模拟器的视频被识别为像该图像的视频.
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.
文件:/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_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'
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!
修改 很抱歉,图片上的最后一个实际上是手动上传的(我从Finder中存储了).因此,模拟器和真实设备都无法上传视频.
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.
推荐答案
我已经解决了.关键是您必须像这样手动指定元数据内容类型.
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);
}
}
这篇关于Flutter- Firebase Storage上传视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!