androidimageviewdata-url

How to load an image from dataurl in android imageview?


I have the following dataurl

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6ODMwQTFBMjFDRkYxMTFFNDk1QzNFODQ3NkZGQzk2MEUiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6ODMwQTFBMjJDRkYxMTFFNDk1QzNFODQ3NkZGQzk2MEUiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4MzBBMUExRkNGRjExMUU0OTVDM0U4NDc2RkZDOTYwRSIgc3RSZ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9rYVbChRbSeBr1F0Hm9DU5RWSC+RFO5Lq2DuZPpryvS5hFknGawxyezF+xpGU3mZUSvD6oVFS/1kSvLGnAtJwL96UqTadyIqC8Wx/PRPLKmbloOrGpzzQCQWRB1vvvZJPMxNkR1kucvGk5YJaxwjM8fJ9YbhPn0ybzFTK+vZ/ZxjSR0Wk4L1UU5hpep6g86xetNuuira4Iky6vo9aSrS9WXDXcuasIQ63RG23c60mTwo3Hy01hHvZqE1myTvIS/yXXuC6DYRWivMTstbEzOSV9LQW2rqcl0Hc6yd/U2rjvF/gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH7IdixtjXannItg1w8WxhBniq59rIr/I3OmwiLGHraC/hW/a2s/n+ixg419b8AHyCGES4X6WNZom+2anff7uZwxooP4EQq6XK7irn8aqDIE+yInN3Z58t+v8cdEd7J/7X2wvs5JfyN7Vyfrv9Nmgl2RDHIEKOpCAhAMmj5+KJBGjCC26IPoIhCMBqiKhPFjRHI4BmwmMRgNmyENRiwMNkMZzHadkgGbCQzmSwyfz0PewRvMMx2+jshUJT2YY+jokHqIDcalQn14sspk7ZAaYTPBECmwlA02c9WbKAcjYTPzSJ2z6pPjn2sz00GS5F3O5+jMAAAAAAAAAAAAAAAAAAAAAADAL/n7nOs09bYCYvG3+WlXLNtCzWNnf0HspptVEEu08HuUhcCZy7tyeHRNnuhkhfSW3etH3vF4rP6ghFHJJW/JPeuNMhMX21ErX8q3exMXRXawqIi+KzJThu/FDW4LdZNk8/u9/OWXYmds1qWNj8aWd/75qxC1

is there a way to load the dataUrl in imageview?


Solution

  • First you need to convert the Base64 encoded string to a Bitmap. Code snippet below from linked question:

    byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
    Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    

    When that is done you have your bitmap in bitmap Assuming your imageViewis defined as R.id.image_view Then just do as you would with any other bitmap:

    ImageView imageView;
    imageView = (ImageView) findViewById(R.id.image_view);
    imageView.setImageBitmap(bitmap);