Firebase 存储图像无法正确上传

标签 firebase flutter firebase-storage

我正在尝试将我从手机拍摄的图像发送到 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)

最佳答案

如果你有文件,为什么要发送文件路径,这对我来说真的没有意义?错误似乎是它没有找到文件的位置。相反,我会做这样的事情:

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);
       },
),

关于Firebase 存储图像无法正确上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312507/

相关文章:

flutter - 如何合并不同类型的 StreamController?

ios - FireBase 存储下载错误域=FIRStorageErrorDomain 代码=-13000

Android Firebase 无法刷新电子邮件验证状态

android - Firebase 中的 uid 不是唯一的

android - 使用 firebase 的用户数据的 json 格式

android - 我可以一次上传 3 个 APK 到 Play 商店吗?

flutter - 在 flutter 中显示透明对话框

firebase - Flutter Firebase 存储插件需要花费大量时间来上传文件

firebase - 在 Firebase 存储中搜索没有扩展名的文件

android - 在 firebase 存储上使用 for 循环上传图像后,如何获取图像的 url?