Flutter 中获取相册图片ByteData出现Unable

2020-12-22  本文已影响0人  溪桥星雨

用了这个插件来获取相册图片,最终得到一个Media数据。不重要,重要的是里面有了图片的Path。

images_picker: ^0.0.6

获取图片的ByteData:

ByteData image = await rootBundle.load(media.path);

调试的时候,没有出错。打包到手机上传图片就提示:
Unable to load asset:xxxxxxxx

最后,百度出了新的解决方案:

Future<Uint8List> _readFileByte(String filePath) async {
    Uri myUri = Uri.parse(filePath);
    File audioFile = new File.fromUri(myUri);
    Uint8List bytes;
    await audioFile.readAsBytes().then((value) {
    bytes = Uint8List.fromList(value); 
    print('reading of bytes is completed');
  }).catchError((onError) {
      print('Exception Error while reading audio from path:' +
      onError.toString());
  });
  return bytes;
}

产生问题的原因就是,rootBundle.load是获取打包进项目的文件。手机里的文件,还是要用Uri来获取。调试的时候,没出错,我估计是跟权限有关。大概是调试的时候,rootBundle.load有能力调用这个。

上一篇下一篇

猜你喜欢

热点阅读