2015-10-16 66 views
0

我有一個應用程序,主要是加載外部URL ...我的問題是: 在我的「HTML在線文件」我怎麼可以本地存儲的資源,例如我試過這個:在我在網上HTML:如何我可以從在線webview加載離線資源

<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離線資源 請幫我:) :)

回答

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