how to load image from expansion file into webview

时间秒杀一切 提交于 2019-11-29 12:17:27

For the benefit of anyone else out there struggling with this, I have a solution, at least for API 11 and greater. I found that ShouldInterceptRequest does in fact get called only if webview.loadUrl(assetFile) doesn't find the file in the asset folder. Perhaps this should have been obvious but I haven't seen it documented anywhere (and I thought I had tried deleting the file but must have done something else wrong) n shouldInterceptRequest looks as follows:

    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url)
    {
        String fileName = url.substring(assetPrefix.length());

        InputStream inputStream = expansionFile.getInputStream(fileName);

        if (url.endsWith("jpg") || url.endsWith("png"))
            return new WebResourceResponse("image/*", "base64", inputStream);

        return super.shouldInterceptRequest(view, url);
    }

Now if anyone has ideas on a solution for API 10 that would still be appreciated. I've seen a suggestion to override shouldOverrideUrlLoading (which we are doing for other purposes) but that doesn't seem to be called when an image is loaded from the html file, only when advancing to a whole new web page

Chris Fawcett

The first answer to How to load an image stored in a byte array to WebView? works for API 10. Thank you Qorbani! Sorry I can't upvote your answer because I don't have the reputation

Christine

Just an addition to Chris Fawcett's answer. The shouldInterceptRequest gets called regularly for loading images that are outside the assets folder. I was stuck up with this for quite some time. Thanks to Chris.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!