Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 that base64 data string in webview using:

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

Sample:

       String urlString = getIntent().getStringExtra("base64String");
       String mimeType = getIntent().getStringExtra("MimeType");

WebSettings settings = webView.getSettings();
    settings.setDefaultTextEncodingName("utf-8");
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
        String base64 = Base64.encodeToString(urlString.getBytes(), Base64.DEFAULT);
        webView.loadData(urlString, "text/html; charset=utf-8", "base64");
    } else {
        String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
        webView.loadData(header + urlString, "text/html; chartset=UTF-8", null);

    }
like image 231
Hareesh Avatar asked Jan 03 '14 06:01

Hareesh


1 Answers

Just Try it:

webView.loadData(urlString, "text/html; charset=utf-8", "base64");
like image 148
Sandip_Jahdav Avatar answered Oct 02 '22 06:10

Sandip_Jahdav