Firebase存储映像未正确上载

Firebase存储映像未正确上载,firebase,flutter,firebase-storage,Firebase,Flutter,Firebase Storage,我正在尝试将我从手机拍摄的图像发送到Firebase存储。第一个函数使用image picker插件获取图像,并将路径返回作为上载函数的参数传入。图像上载到云存储,但在面板中类型为application/octet stream,并且图像不显示 String download_path; var imageFile; picker() async{ File theImage = await ImagePicker.pickImage( source: ImageSource.gall

我正在尝试将我从手机拍摄的图像发送到Firebase存储。第一个函数使用image picker插件获取图像,并将路径返回作为上载函数的参数传入。图像上载到云存储,但在面板中类型为
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());
       },
),
字符串下载路径;
var图像文件;
picker()异步{
File theImage=wait ImagePicker.pickImage(
资料来源:ImageSource.gallery);
图像文件=图像;
var theimagepath=theImage.path;
设置状态(){
图像文件=图像;
});
}
未来上传文件(字符串myfilepath)异步{
最终RegExp RegExp=RegExp(“([^?/]*\(jpg))”;
最终文件名=regExp.stringMatch(myfilepath);
最终目录tempDir=Directory.systemTemp;
最终文件thefile=wait File(“${tempDir.path}/$filename”).create();
final-StorageReference sref=FirebaseStorage.instance.ref().child('storeFolderName').child(文件名);
final StorageUploadTask uploadTask=sref.putFile(文件);
最终Uri downloadUrl=(等待uploadTask.future);
download_path=downloadUrl.toString();
打印(“下载url打印:$download_path”);
}
图标按钮(
图标:图标(Icons.cloud_done),
onPressed:(){上传文件(imageFile.toString());
},
),
日志输出:
D/Surface(18601):Surface::setBufferCount(this=0x9272d800,bufferCount=4)
D/GraphicBuffer(18601):寄存器,句柄(0x97ee29c0)(w:480 h:854 s:480 f:0x1 u:f02)
D/GraphicBuffer(18601):寄存器,句柄(0x97ee2e40)(w:480 h:854 s:480 f:0x1 u:f02)
D/GraphicBuffer(18601):寄存器,句柄(0x8ea20140)(w:480 h:854 s:480 f:0x1 u:f02)
W/System(18601):类加载器引用的未知路径:System/framework/mediatek-cta.jar
I/System.out(18601):e:java.lang.ClassNotFoundException:com.mediatek.cta.CtaHttp

I/System.out(18601):[OkHttp]sendRequest如果你有这个文件,为什么要发送这个文件的路径对我来说没有意义?错误似乎是找不到文件的位置。相反,我会这样做:

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);
       },
),
字符串下载路径;
var图像文件;
picker()异步{
File theImage=wait ImagePicker.pickImage(
资料来源:ImageSource.gallery);
图像文件=图像;
var theimagepath=theImage.path;
设置状态(){
图像文件=图像;
});
}
未来上传文件(文件myFile)异步{
最终存储参考sref=
FirebaseStorage.instance.ref().child('storeFolderName').child(myFile.toString());
final StorageUploadTask uploadTask=sref.putFile(myFile);
最终Uri downloadUrl=(等待uploadTask.future);
download_path=downloadUrl.toString();
打印(“下载url打印:$download_path”);
}
图标按钮(
图标:图标(Icons.cloud_done),
onPressed:(){上传文件(imageFile);
},
),

我也有同样的问题。自动检测mime类型似乎不起作用,因此我最终使用mime包并在StorageMetadata中发送mime类型。

我也面临这个问题,两天后,通过添加元数据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,图像将作为应用程序/八位字节流(仅在iOS上)上载。但是有了元数据,我就可以工作了。希望对您有所帮助。

请发布与上传相关的任何日志/异常。