如何将图像从扩展文件加载到webview中 [英] how to load image from expansion file into webview

查看:209
本文介绍了如何将图像从扩展文件加载到webview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用程序中,我们有html页面,从资产目录加载到webview中。这些反过来从同一个地方加载jpg图像。由于Google Play的应用程序大小限制为50毫克,我需要将jpg移动到扩展文件中。出于安全原因,我们不想移动html页面。任何人都可以告诉我如何将扩展文件中的jpg加载到html网页中?

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?

谢谢

Christine

Christine

还有一些想法:

这个链接发布了一个类似的问题,但仍然没有答案:

This link posts a similar question but remains unanswered:

在android webview中使用apk扩展文件中的图像

此链接确实有答案,但它们似乎不完整,至少对我的需求而言:

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

我们如何使用APK主扩展文件中的图像?

我在想扩展文件的URI接口(APEZProvider为ExpansionFile创建一个ContentProvider,参见 http://developer.android.com/guide/google/play/expansion-files.html#ZipLib )可能是要走的路,但我是一个我不清楚如何获取Javascript的信息。目前我们正在使用:

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:

我已尝试重写ShouldInterceptRequest,如webview shouldinterceptrequest示例
期待在图片即将加载时获得回调,这样我就可以修改url,但是不会调用shouldInterceptRequest。

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.

那么,有没有人有任何想法如何拦截jpg的加载并将网址重新定向到从扩展文件的ContentProvider获得的网址?

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

推荐答案

为了其他任何人在这方面苦苦挣扎的利益,我有一个解决方案,至少对于API 11和更高版本。我发现只有当webview.loadUrl(assetFile)在资产文件夹中找不到该文件时,ShouldInterceptRequest实际上才会被调用。也许这应该是显而易见的,但我没有看到它记录在任何地方(我以为我曾尝试删除该文件,但必须做其他错误)
n shouldInterceptRequest如下所示:

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);
    }

现在,如果有人对API 10的解决方案有任何想法仍然会受到赞赏。我已经看到了一个覆盖shouldOverrideUrlLoading(我们正在为其他目的做)的建议,但是当从html文件加载图像时似乎没有调用,只有当它进入一个全新的网页时才会被调用

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

这篇关于如何将图像从扩展文件加载到webview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆