Firebase 存储图像无法正确上传
Posted
技术标签:
【中文标题】Firebase 存储图像无法正确上传【英文标题】:Firebase Storage image does not upload properly 【发布时间】:2018-12-21 02:09:54 【问题描述】:我正在尝试将我从手机拍摄的图像发送到 Firebase 存储。第一个函数使用图像选择器插件获取图像并将路径返回作为上传函数的参数传递。图像上传到云存储,但在面板中类型为application/octet-stream
,图像不显示
String download_path;
var imageFile;
picker() async
File theImage = await ImagePicker.pickImage(
source: ImageSource.gallery);
imageFile = theImage;
var theimagepath = theImage.path;
setState(()
imageFile = theImage;
);
Future<Null> uploadFile(String myfilepath)async
final RegExp regExp = RegExp('([^?/]*\.(jpg))');
final filename = regExp.stringMatch(myfilepath);
final Directory tempDir = Directory.systemTemp;
final File thefile = await File('$tempDir.path/$filename').create();
final StorageReference sref = FirebaseStorage.instance.ref().child('storeFolderName').child(filename);
final StorageUploadTask uploadTask = sref.putFile(thefile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
download_path = downloadUrl.toString();
print('download url printed : $download_path');
IconButton(
icon: Icon(Icons.cloud_done),
onPressed: ()uploadFile(imageFile.toString());
,
),
日志输出:
D/Surface (18601): Surface::setBufferCount(this=0x9272d800,bufferCount=4)
D/GraphicBuffer(18601): register, handle(0x97ee29c0) (w:480 h:854 s:480 f:0x1 u:f02)
D/GraphicBuffer(18601): register, handle(0x97ee2e40) (w:480 h:854 s:480 f:0x1 u:f02)
D/GraphicBuffer(18601): register, handle(0x8ea20140) (w:480 h:854 s:480 f:0x1 u:f02)
W/System (18601): ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
I/System.out(18601): e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttp
I/System.out(18601): [OkHttp] sendRequest<<
D/GraphicBuffer(18601): register, handle(0x8ea21040) (w:480 h:854 s:480 f:0x1 u:f02)
W/System (18601): ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
I/System.out(18601): e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttp
I/System.out(18601): [OkHttp] sendRequest<<
I/flutter (18601): download url printed : https://firebasestorage.googleapis.com/v0/b/cloud-fs-demo.appspot.com/o/storeFolderName%2FIMG_20180711_080138.jpg?alt=media&token=6fb05871-04df-458d-93bc-1951cd122770
E/[EGL-ERROR](18601): __egl_platform_cancel_buffers:644: surface->num_buffers(4)
【问题讨论】:
请发布与上传相关的任何日志/异常情况。 【参考方案1】:如果你有文件,为什么要发送文件的路径对我来说真的没有意义?似乎错误是它找不到文件的位置。相反,我会这样做:
String download_path;
var imageFile;
picker() async
File theImage = await ImagePicker.pickImage(
source: ImageSource.gallery);
imageFile = theImage;
var theimagepath = theImage.path;
setState(()
imageFile = theImage;
);
Future<Null> uploadFile(File myFile)async
final StorageReference sref =
FirebaseStorage.instance.ref().child('storeFolderName').child(myFile.toString());
final StorageUploadTask uploadTask = sref.putFile(myFile);
final Uri downloadUrl = (await uploadTask.future).downloadUrl;
download_path = downloadUrl.toString();
print('download url printed : $download_path');
IconButton(
icon: Icon(Icons.cloud_done),
onPressed: ()uploadFile(imageFile);
,
),
【讨论】:
【参考方案2】:我也有同样的问题。自动检测 mime 类型似乎不起作用,所以我最终使用 mime 包并在 StorageMetadata 中发送 mime 类型。
【讨论】:
【参考方案3】:我也遇到了这个问题,两天后终于通过添加 metaData、contentType 解决了这个问题。有趣的是,在我的情况下,相同的代码适用于 android,但适用于 ios 是错误的。 所以这是我使用的代码 sn-p:
final File selectedImage = await ImagePicker.pickImage(
source: ImageSource.gallery,
);
filePath = selectedImage.path;
currentFile = selectedImage;
final StorageReference storageRef =
FirebaseStorage.instance.ref().child('images');
final StorageUploadTask task = storageRef.child('myImage.jpeg').putFile(selectedImage, StorageMetadata(contentType: 'image/jpeg'));
await task.onComplete; // do something
因此,如果没有 putFile 方法的 StorageMetadata,图像将作为 application/octet-stream 上传(仅在 iOS 上)。但是使用元数据对我来说工作得很好。希望对您有所帮助。
【讨论】:
以上是关于Firebase 存储图像无法正确上传的主要内容,如果未能解决你的问题,请参考以下文章
如何正确设置 CORS 以将带有 presignedUrl 的文件上传到 Firebase 存储?
无法使用 firebase 功能将图像上传到 firebase 存储
无法从 python 服务器将图像上传到 Firebase 存储
将图像存储在 Firebase Storage 中并将元数据保存在 Firebase Cloud Firestore(测试版)中