我实际上是想将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);
拜托,有人可以告诉我,我犯了什么错误。
非常感谢,Mahi
嗨,我刚刚更改了我的代码如下,
List<int> imageBytes = widget.fileData.readAsBytesSync();
print(imageBytes);
String base64Image = base64Encode(imageBytes);
这现在工作正常。
最好以异步方式读取它,因为图像可能非常大,这可能会导致主线程被阻塞
List<int> imageBytes = await widget.fileData.readAsBytes();