【发布时间】:2018-04-26 06:41:28
【问题描述】:
我实际上是在尝试将ImagePicker 在颤振中挑选的图像转换为base64 图像。我总是收到错误。
FileSystemException: Cannot open file, path =
'file:///storage/emulated/0/Download/Abid_Wipro_neemuchwala1-
770x433.jpg' (OS Error: No such file or directory, errno = 2)
E/flutter ( 5042): #0 _File.throwIfError
(dart:io/file_impl.dart:628)
E/flutter ( 5042): #1 _File.openSync
(dart:io/file_impl.dart:472)
E/flutter ( 5042): #2 _File.readAsBytesSync
(dart:io/file_impl.dart:532)
我使用的代码就是这个。
File fileData;
/////////////...........
new Container(
child: new FutureBuilder<File>(
future: imageFile,
builder: (BuildContext context, AsyncSnapshot<File> snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
snapshot.data != null) {
fileData = snapshot.data;
return new Container(
height: MediaQuery.of(context).size.height / 2,
width: MediaQuery.of(context).size.width,
margin: const EdgeInsets.all(4.0),
decoration: new BoxDecoration(
image: new DecorationImage(
image: new FileImage(snapshot.data,),
fit: BoxFit.cover
),
),
);
} else if (snapshot.error != null) {
return new Column(children: <Widget>[
centerWidget('Choose Image or Audio or Video'),
_circleAvatar()
]);
} else {
return new Column(children: <Widget>[
centerWidget('Choose Image or Audio or Video'),
_circleAvatar()
]);
}
},
),
),
/////////////////
File imageFile = new File(widget.fileData.uri.toString());
List<int> imageBytes = imageFile.readAsBytesSync();
String base64Image = base64Encode(imageBytes);
拜托,谁能告诉我我在哪里犯了错误。
非常感谢, 鲯鳅
【问题讨论】:
-
您尝试读取的文件不存在或应用没有读取权限。该错误似乎与 base64 无关
-
Thanks@GünterZöchbauer 但文件存在于指定路径中,并显示在手机上但无法将图像转换为 base64
-
报错说无法读取文件时还是和base64无关。
-
嗨@zoechi 还有其他解决方案,以便我可以将图像文件转换为base64编码。
-
感谢@zoechi,您太棒了,路径已被修改,我删除了
File imageFile = new File(widget.fileData.uri.toString());行并将我的代码更改为List<int> imageBytes = widget.fileData.uri.readAsBytesSync();,它现在可以工作了。