skip to Main Content

This is my code

var byte = File(item.path).readAsBytesSync();
        String img64 = base64Encode(byte);

And I have a result like:

(/9j/4TY3RXhpZgAASUkqAAgAAAALADIBAgAUAAAAkgAAABABAgAaAAAApgAAAAABBAABAAAAwAMAABMCAwABAAAAAQAAACgBAwABAAAAAgAAAAEBBAABAAAAwAMAABsBBQABAAAAwAAAABIBAwABAAAAAQAAAGmHBAABAAAA1wAAABoBBQABAAAAyAAAAA8BAgAHAAAA0AAAALUBAAAyMDIzOjExOjIxIDEzOjMyOjQ5AEFuZHJvaWQgU0RLIGJ1aWx0IGZvciB4ODYASAAAAAEAAABIAAAAAQAAAEdvb2dsZQAOAAGgAwABAAAA)

But I want to have a result like this:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3gAAALyCAYAAACIMnZEAAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvXd8XMW9sP/MOWertGqWrGbLBSPbuIALtummGXAw5QIhJr/7JpDCJYSEAJeUl18wgRtyEyAJgSSQkJBGCwkdnFBMCS64gXvvsrosabX9nDPvH2d3t

2

Answers


  1. you just need to concatenate data:image/png;base64, with base64 String
    like this:

    var byte = File(item.path).readAsBytesSync();
    String img64 = 'data:image/png;base64,' + base64Encode(byte); // change png with the image extension you have 
    
    Login or Signup to reply.
  2. The Dartk SDK includes a UriData class that can do it for you.

    var bytes = File(item.path).readAsBytesSync();
    var uriData = UriData.fromBytes(bytes, mimeType: 'image/png');
    

    From there, you can use uriData.toString() to get a String representation or uriData.uri to get a Uri object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search