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

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

问题描述

在我们的应用程序中,我们有从资产目录加载到 webview 的 html 页面.这些反过来从同一个地方加载jpg图像.由于 Google Play 的 50mg 应用程序大小限制,我需要将 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?

谢谢

克里斯汀

还有一些想法:

此链接发布了一个类似的问题,但仍未得到答复:

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 的加载并将 url 重定向到从扩展文件的 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天全站免登陆