Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert an image to base64 image in flutter?

Tags:

flutter

dart

I am actually trying to convert an image picked by ImagePicker in flutter to base64 image. I am always getting the error.

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) 

the code i am using is this one.

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

Please, can someone tell me where is it that i am making a mistake .

Many thanks, Mahi

like image 698
Mahi Avatar asked Apr 26 '18 06:04

Mahi


People also ask

Can we convert image to base64?

Convert Images to Base64Just select your JPG, PNG, GIF, Webp, or BMP picture or drag & drop it in the form below, press the Convert to Base64 button, and you'll get a base-64 string of the image. Press a button – get base64. No ads, nonsense, or garbage. Drag and drop your image here!

How do you decode base64 in flutter?

For base64 decoding, use one of the following methods: Uint8List base64. decode(String input) Uint8List base64Decode(String input)


1 Answers

I just changed my code as follows,

import 'dart:convert';  List<int> imageBytes = widget.fileData.readAsBytesSync(); print(imageBytes); String base64Image = base64Encode(imageBytes); 

and this is working fine now.

It is better to read it asynchronously as the image can be very large which may cause blocking of the main thread

 List<int> imageBytes = await widget.fileData.readAsBytes(); 
like image 51
Mahi Avatar answered Sep 22 '22 02:09

Mahi