简体   繁体   中英

how i can load offline resource from online webview

i have an application that mainly load external url ... my question is : in my "html online file " how i can local stored resource for example i tried this :in my online 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"/>

but its not work ... ** i know how to load whole webview locally offline but this is online webview with offline resource please help me :) :)

You can't due to same origin policy, Its a security breach !! For more details !

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

You can interface with the apk and maybe have the apk give you the bytes for the file

// 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>

Further reading: http://developer.android.com/guide/webapps/webview.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM