Kod*_*ata 7 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);
}
}
Run Code Online (Sandbox Code Playgroud)
但问题是我上传了 3 个不同的视频。一个来自真实设备,其他来自 Ios 模拟器,只有一个来自模拟器的视频被识别为这样的视频。
文件:/Users/Daibaku/Library/Developer/CoreSimulator/Devices/C99406E4-12F3-480A-82A6-F6144ADD21AB/data/Containers/Data/Application/23E82E18-9293-4EEB-AEEA-6A7BB773F2F2-6A7BB773F2F2-6A7BB773F2F2-12F3-480A-82A6-F6144ADD21AB -BA8E-5F219374D8C8-7394-000006A2FA530CD0.MOV'
文件:'/Users/Daibaku/Library/Developer/CoreSimulator/Devices/C99406E4-12F3-480A-82A6-F6144ADD21AB/data/Containers/Data/Application/23E82E18-9293-4EEB-AEEA-6A7F975C5-12F3-480A-82A6-F6144ADD21AB 4804-9312-69E1696CAF87-7394-000006A80D46F0B7.MOV'
这些是来自模拟器的文件路径,底部的一个被识别为视频。有谁知道发生了什么以及如何解决它?谢谢!
编辑 抱歉,图片上的最后一张是手动上传的(我从我的查找器中放入了存储)。所以模拟器和真机都不能上传视频。
我解决了。关键是您必须像这样手动指定元数据内容类型。
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6507 次 |
| 最近记录: |