How to display the byte array data in webview on Android

2.5k views Asked by At

I have the HTML data in the byte array, and I want to display it in webview. Currently I am displaying it by converting the byte array to a string and displaying it by using the following code.

mDecryptDataWv.loadDataWithBaseURL("", htmlData,    mimeType, encoding, "");

I want to display the byte array directly into webview without using a string.

1

There are 1 answers

2
Sush On

Sorry, ultimately you can't do that if you're using below API level 11. Even the webview loads data interms of string only (basically it parses the HTML tags and renders the content). Even when you load the URL in webview also the same thing happens.

If you're using 11 and above, you can use the following method.

@TargetApi(11)
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    Log.d("shouldInterceptRequest", url);

    InputStream stream = new ByteArrayInputStream(source);
    if (stream != null) {
        return new WebResourceResponse("text/javascript", "utf-8", stream);
    }
    return super.shouldInterceptRequest(view, url);
}