2016-12-15 71 views
0

我的應用程序需要從服務器下載一個zip文件,解壓縮並使用webview中的文件。從一個下載的文件的Webview

我已經完成了上述所有步驟,但webview只顯示html。 CSS和JavaScript不起作用。

所有的路徑都是相對的,這些文件在瀏覽器上工作。

的網頁流量的代碼是這樣的

WebSettings ws = myWebView.getSettings(); 
    ws.setAllowFileAccess(true); 
    String dir = getFilesDir().getAbsolutePath(); 
    String saveDir = dir+"/Download/"; 
    Toast.makeText(getApplicationContext(),"file:/"+ saveDir+"352_1332.html" ,Toast.LENGTH_LONG).show(); 
    myWebView.loadUrl("file://"+saveDir+"352_1332.html"); 
+0

[Android中WebView中使用JavaScript(的可能的複製http://stackoverflow.com/questions/10472839 /使用-java-in-android-webview) –

回答

0

你必須解壓縮文件在手機spesific位置,並給該文件夾路徑

webView.loadDataWithBaseURL("file://"+outPutFolder, finalDataToLoad, "text/html", "utf-8", null); 

這裏outputFolder的文件夾路徑,finalDataToLoad是您要加載的數據/ html

private void DownloadResource(String directory) { 
try { 

    Resources rst = book.getResources(); 
    Collection<Resource> clrst = rst.getAll(); 
    Iterator<Resource> itr = clrst.iterator(); 

    while (itr.hasNext()) { 
     Resource rs = itr.next(); 

     if ((rs.getMediaType() == MediatypeService.JPG) 
             || (rs.getMediaType() == MediatypeService.PNG) 
             || (rs.getMediaType() == MediatypeService.GIF)) { 

      Log.d("Href", rs.getHref()); 

      File oppath1 = new File(directory, rs.getHref().replace("OEBPS/", ""));  

      oppath1.getParentFile().mkdirs(); 
      oppath1.createNewFile(); 

      System.out.println("Path : "+oppath1.getParentFile().getAbsolutePath()); 


      FileOutputStream fos1 = new FileOutputStream(oppath1); 
      fos1.write(rs.getData()); 
      fos1.close(); 

     } else if (rs.getMediaType() == MediatypeService.CSS) { 

      File oppath = new File(directory, rs.getHref()); 

      oppath.getParentFile().mkdirs(); 
      oppath.createNewFile(); 

      FileOutputStream fos = new FileOutputStream(oppath); 
      fos.write(rs.getData()); 
      fos.close(); 

     } 

    } 


} catch (Exception e) { 

} 
} 

這裏的書是我的epubfile

+0

finalDataToLoad需要是一個字符串對嗎?我必須將每個文件轉換爲字符串? –

+0

從您的html頁面獲取所有數據並將其存儲在字符串中, – Redman

+0

解壓縮請按照此http://stackoverflow.com/a/40124766/6478047 – Redman

0

我猜WebView查找資源(CSS,JS)出錯了。 例如您352_1332.html看起來是這樣的:

<html> 
 
<head> 
 
    <link rel="stylesheet" href="stylesheet.css"> 
 
</head> 
 
<body> 
 
    <!-- some elements --> 
 
</body> 
 
</html>

默認web視圖不會嘗試你SaveDir可以目錄下找到stylesheet.css中。你必須指向文件。要做到這一點,你必須覆蓋WebViewClient.shouldInterceptRequest:以上

myWebView.setWebViewClient(new WebViewClient() { 

    @Override 
    public WebResourceResponse shouldInterceptRequest (WebView view, String url) { 

     //url = stylesheet.css -> now point to resource file 
     FileInputStream data = null; 
     try { 

      data = new FileInputStream("file://" + saveDir + File.separator + url)); 
     } catch (FileNotFoundException e) { 

      return null; 
     } 
     return new WebResourceResponse(this.getMimeTyp(url), null, data); 
    } 
} 

/** 
* returns mime-typ 
* @param url String 
* @return String 
*/ 
private String getMimeTyp (String url) { 

    if (url.endsWith(".css") == true) { 

     return "text/css"; 
    } else if (url.endsWith(".js") == true) { 

     return "text/javascript"; 
    } else if (url.endsWith(".html") == true) { 

     return "text/html"; 
    } else if (url.endsWith(".png") == true) { 

     return "image/png"; 
    } else if (url.endsWith(".jpg") == true || url.contains(".jpeg") == true) { 

     return "image/jpeg"; 
    } else if (url.endsWith(".gif") == true) { 

     return "image/gif"; 
    } else { 

     //fallback for everything else 
     return "application/octet-stream"; 
    } 
} 

代碼是完全未經測試...

相關問題