Skip to content Skip to sidebar Skip to footer

How To Load Any Format Of Base64 Data To Webview In Android?

I am implementing an android application related to Webview. I am getting base64 data string from server that data format may be jpg or pdf file or doc file etc. I want to load tha

Solution 1:

Just Try it:

webView.loadData(urlString, "text/html; charset=utf-8", "base64");

Solution 2:

please have alook at https://github.com/gregko/WebArchiveReader for a good example and hope it may help you.

byte[] imageRaw = null;
  try {
     URLurl=newURL("http://some.domain.tld/somePicture.jpg");
     HttpURLConnectionurlConnection= (HttpURLConnection) url.openConnection();

     InputStreamin=newBufferedInputStream(urlConnection.getInputStream());
     ByteArrayOutputStreamout=newByteArrayOutputStream();

     int c;
     while ((c = in.read()) != -1) {
         out.write(c);
     }
     out.flush();

     imageRaw = out.toByteArray();

     urlConnection.disconnect();
     in.close();
     out.close();
  } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }

  Stringimage64= Base64.encodeToString(imageRaw, Base64.DEFAULT);

  StringurlStr="http://example.com/my.jpg";
  StringmimeType="text/html";
  Stringencoding=null;
  StringpageData="<img src=\"data:image/jpeg;base64," + image64 + "\" />";

  WebView wv;
  wv = (WebView) findViewById(R.id.webview);
  wv.loadDataWithBaseURL(urlStr, pageData, mimeType, encoding, urlStr);

Post a Comment for "How To Load Any Format Of Base64 Data To Webview In Android?"