正在尝试将我从电话中获取的图像发送到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.jarI/System.out(18601): e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttpI/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.jarI/System.out(18601): e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaHttpI/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-1951cd122770E/[EGL-ERROR](18601): __egl_platform_cancel_buffers:644: surface->num_buffers(4)
最佳答案
我也面临这个问题,两天后,我终于通过添加元数据contenttype来解决这个问题。有趣的是,在我的例子中,相同的代码适用于Android,但在iOS中是错误的。
下面是我使用的代码片段:
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流的形式上载(仅在iOS上)。但是元数据对我来说很好。希望有帮助。