I am actually trying to convert an Image file to Base64 and decode Base64 String to Image file in Dart/Flutter. Indeed, my application takes pictures with the camera plugin and then, saves it in a database as a Base64 String (because I don't know any other way but may be there is more relevant ways to store several images?).
I encode with this code :
final path = join(
(await getTemporaryDirectory()).path,
'${DateTime.now()}.png',
);
await _controller.takePicture(path);
final bytes = File(path).readAsBytesSync();
String img64 = base64Encode(bytes);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(imageAnalysed: img64),
),
);
And decode with this one :
class DisplayPictureScreen extends StatelessWidget {
final String imageAnalysed;
const DisplayPictureScreen({Key key, this.imageAnalysed}) : super(key: key);
@override
Widget build(BuildContext context) {
final decodedBytes = base64Decode(imageAnalysed);
var fileImg= File("testImage.png");
fileImg..writeAsBytesSync(decodedBytes);
return Scaffold(
appBar: AppBar(title: Text('Display the Picture')),
body: Image.file(fileImg),
);
}
}
But there is this error :
The following FileSystemException was thrown building DisplayPictureScreen(dirty):
Cannot open file, path = 'testImage.png' (OS Error: Read-only file system, errno = 30)
How can I solve my problem and get an image from the Base64 String ?
Thank you for your help !
writeAsBytesSync
. – maximevar fileImg= File("testImage.png")
created a temporary file with a particular directory. So to solve the problem I should usepath_provider
to implementgetTemporaryDirectory()
and create a temporary file at this directory ? Or is there something simpler ? – maxime