3

I'm trying to convert an image byte to base64 encoded string, this operation is successfull but when i print out the encoded string i get the ff:

/9j/4RgvRXhpZgAASUkqAAgAAAANAAABBAABAAAAQAYAAAEBBAABAAAAsAQAAA8BAgAIAAAAqgAAABABAgAIAAAAsgAAABIBAwABAAAACAAAABoBBQABAAAAugAAABsBBQABAAAAwgAAACgBAwABAAAAAgAAADEBAgANAAAAygAAADIBAgAUAAAA2AAAABMCAwABAAAAAQAAAGmHBAABAAAA7AAAACWIBAABAAAA7gIAAAADAABzYW1zdW5nAFNNLVA1NTUASAAAAAEAAABIAAAAAQAAAFA1NTVYWFUxQ1JBMgAAMjAxOTowMToxNCAwOTozMzoyOAAZAJqCBQABAAAAHgIAAJ2CBQABAAAAJgIAACKIAwABAAAAAgAAACeIAwABAAAAZAAAAACQBwAEAAAAMDIyMAOQAgAUAAAALgIAAASQAgAUAAAAQgIAAAGRBwAEAAAAAQIDAAKSBQABAAAAVgIAAAWSBQABAAAAXgIAAAeSAwABAAAAAgAAAAiSAwABAAAAAAAAAAmSAwABAAAAAAAAAAqSBQABAAAAZgIAAHySBwBiAAAAbgIAAACgBwAEAAAAMDEwMAGgAwABAAAAAQAAAAKgBAABAAAAQAYAAAOgBAABAAAAsAQAAAWgBAABAAAA0AIAABeiAwABAAAAAgAAAAGjBwABAAAAAQAAAAKkAwABAAAAAAAAAAOkAwABAAAAAAAAAAakAwABAAAAAAAAAAAAAAABAAAAFAAAAPAAAABkAAAAMjAxOTowMToxNCAwOTozMzoyOAAyMDE5OjAxOjE0IDA5OjMzOjI4AP0AAABkAAAA/QAAAGQAAADrAAAAZAAAAAcAAQAHAAQAAAAwMTAwA

An encoded string is supposed to be way more than the characters above. Now when i try to view this by appending data:image/jpeg;base64, to the begin of the encoded string, nothing gets displayed.

Is flutter cutting the string in the console or what and if yes then how do i get the complete encoded string.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
marvin ralph
  • 1,100
  • 3
  • 23
  • 43

5 Answers5

1

You should convert it to Bytes if you're trying to send it to the backend. This will give you the long string you're looking for ;)

var params = {
      "image_file": base64Encode(_selectedImage.readAsBytesSync()),
    };
Leoog
  • 244
  • 2
  • 8
1

For me base64 is not showing completely. if that the case, print() and debugPrint() both are used for logging in the console. If you are use print() and output is too much at once, then Android sometimes discards some log lines. To avoid this, use debugPrint().

Shahryar Rafique
  • 1,211
  • 15
  • 35
0

How to native convert string -> base64 and base64 -> string shows how to base64-encode

Dart also provides constructors on the Uri class to get a data URL

where the former does the base64-encoding for you and the later takes an already encoded string.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
0

Probably the debug output windows is truncating it since it's too long. Please make sure on API side whether you take it completely or not.

Volki Tolki
  • 19
  • 1
  • 3
-2

here is my code

static Future<Response> upload64(File file) 
    List<int> imageBytes = file.readAsBytesSync();
    String base64Image = base64Encode(imageBytes);

    String fileName = path.basename(file.path);

    var body = {"fileName": fileName, "base64": base64Image};
    print("http.upload >> " + body.toString());

  }
Lomtrur
  • 1,703
  • 2
  • 19
  • 35