0

I am trying to display an image encoded in base64 in a WebView. It's working for most of my users but a few are seeing only weird characters, like shown in the image below.

This is how I display the image:

String b64Image = Base64.encodeToString(fileData, Base64.DEFAULT);
mWebView.loadData(b64Image, "image/jpeg", "base64");

The problem occurs on android version 2.3.6 and lower.

Marius
  • 309
  • 1
  • 5
  • 16
  • 2
    I'm surprised that this works anywhere. Why are you loading it this way, rather than showing the image in an `ImageView`? – CommonsWare Feb 23 '14 at 19:08
  • The Image I am trying to load is very big and causes out of memory errors If it's not being loaded into a WebView. Also in a WebView you can smoothly zoom and scroll the image. – Marius Feb 23 '14 at 19:11

1 Answers1

1

For Android 2.2 and below you could wrap the image with HTML and load the HTML instead. For example:

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
    // Your original code here
   } else {
      byte[] imageRaw = yourImage;
      String image64 = Base64.encodeToString(imageRaw, Base64.DEFAULT);
      String pageData = "<img src=\"data:image/jpeg;base64," + image64 + "\" />";
      //.... etc.
   }

There was a bug opened on this which was closed on 2010: https://code.google.com/p/android/issues/detail?id=596

Community
  • 1
  • 1
georgehdd
  • 433
  • 3
  • 12