2

In our app we have html pages which are loaded into webview from the asset directory. These in turn load jpg images from the same place. I need to move the jpg's to an expansion file due to the 50mg app size limitation of Google Play. We don't want to move the html pages, for security reasons. Can anyone tell me how to load a jpg from an expansion file into the html webpage?

Thanks

Christine

A few more thoughts:

This link posts a similar question but remains unanswered:

Using images from apk expansion file in android webview

This link does have answers but they seem incomplete, at least for my needs:

How we can use images from APK main expansion file?

I am thinking that the URI interface to the expansion file (APEZProvider creates a ContentProvider for the ExpansionFile, see http://developer.android.com/guide/google/play/expansion-files.html#ZipLib) might be the way to go but I am unclear how to get the information to the Javascript. Currently we are using:

I've tried overriding ShouldInterceptRequest, as suggested in webview shouldinterceptrequest example, expecting to get a callback when the image is about to be loaded, so that I could then modify the url, but shouldInterceptRequest doesn't get called.

So, does anybody have any ideas how to intercept the loading of the jpg and re-direct the url to the one obtained from the Expansion File's ContentProvider?

Any other suggestions short of unzipping the expansion file? Any help would be much appreciated. I've spent a few days researching this now

Community
  • 1
  • 1
Chris Fawcett
  • 161
  • 3
  • 11
  • Possible duplicate of [How to load an image stored in a byte array to WebView?](http://stackoverflow.com/questions/4495673/how-to-load-an-image-stored-in-a-byte-array-to-webview) – zondo Jul 27 '16 at 11:28
  • Absolutely not. This one is about how to access an image stored in an expansion file. You can just open the *jpg file when the file is buried in a zipped expansion file – Chris Fawcett Jul 27 '16 at 21:27

3 Answers3

4

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
  • 161
  • 3
  • 11
  • To clarify, webview.loadUrl(assetFile) does load the html file from the asset folder. But the html file loads images files which are in the expansion file. These get loaded now from ShouldInterceptRequest which is called when the image is not found in the asset folder – Chris Fawcett Oct 30 '12 at 15:58
0

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

Community
  • 1
  • 1
Chris Fawcett
  • 161
  • 3
  • 11
  • Stack Overflow makes it easy to link one question to another. I have voted to close this question as a duplicate of the one you linked. At the top of the question, there should be a banner asking you if this link solved your question. Since, of course, it did, you can click the button that it did. Once you do that, it will be much easier for people passing through to notice the link, and you can delete this answer. – zondo Jul 27 '16 at 11:30
0

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.

Agnit
  • 141
  • 7