我如何从在线webview加载离线资源

时间:2015-10-16 15:14:00

标签: android html5 webview

我有一个主要加载外部网址的应用程序... 我的问题是: 在我的“html在线文件”中我如何可以本地存储资源,例如我试过这个:在我的在线HTML:

<img src = "file:///android_asset/fav/1.png"/>
and 
<img src = "file:///android_asset/fav/1.png"/>
and 
<img src = "./android_asset/fav/1.png"/>

但它不起作用...... **我知道如何在线下加载整个webview 但这是带有线下资源的在线webview 请帮帮我:) :)

2 个答案:

答案 0 :(得分:0)

由于相同的原始政策,您无法进行安全漏洞!有关详细信息!

https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy

答案 1 :(得分:0)

你可以与apk接口,也许让apk给你文件的字节

// for your webview
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");

// js bridge interface
public class WebAppInterface {

    /** Get bytes of a file from the web page */
    @JavascriptInterface
    public byte[] getFileBytes(String path) {
        InputStream stream = Context.openFileInput(new File(path));
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        while ((int read = stream.read(bytes)) != -1) {
           byteOutputStream.write(bytes, 0, read);
        }
        return byteOutputStream.toByteArray();
    }
}

// to call the method in html
<script type="text/javascript">
    function androidGetFileBytes(path) {
        def bytes = Android.getFileBytes(path);
        // TODO Do something with bytes
    }
</script>

进一步阅读:http://developer.android.com/guide/webapps/webview.html

相关问题