2012-08-01 50 views
1

我想用一些圖片在我的webview中設置一些html內容。 我已經在互聯網上閱讀我必須使用loadDataWithBaseUrl做這樣的事情,因爲我需要連接圖像文件夾(basUrl)按順序。使用webView的loadDataWithBaseURL不加載SD卡中的圖片

我的html內容很好地加載,我甚至可以完美地運行javascripts,但由於某種原因我的圖像無法加載。

某處我讀過存在一定的安全理由,這就是爲什麼我不能加載圖片來自SD卡的WebView有的說,它可以通過loadDataWithBaseUrl輕鬆完成,所以我真的不知道哪一個是真的。

這裏是我的方法我試過了,也許有一些錯誤,所以不要粗魯:

我來到這裏我的html文件:

mnt/sdcard/com.mypackage.myproject/3/3.html 

我的圖片在這裏:

mnt/sdcard/com.mypackage.myproject/3/images/Cover.png 

這是我的內容loading:

myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", ""); 

在我的html代碼:

<img src="images/Cover.png" alt="Cover.png" height="820"/> 

所以你看我給的baseUrl,以及由於某種原因的WebView不能加載我的圖片。

正如許多說,這可能是一個解決辦法:

mWebView = (WebView) findViewById(R.id.webview); 
mWebView.getSettings().setAllowFileAccess(true); 
mWebView.getSettings().setJavaScriptEnabled(true); 
mWebView.getSettings().setBuiltInZoomControls(true); 
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); 
String imagePath = "file:/"+ base + "/test.jpg"; 
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>"; 
mWebView.loadData(html, "text/html","utf-8"); 

但是,我有700個不同的HTML文件,有很多圖像許多不同的地方......所以我不能修改HTML代碼。

有沒有辦法將圖像文件夾鏈接到我的html文件,以正確使用它們作爲baseUrl?

+0

你看http://stackoverflow.com/questions/3069822/is-it-possible-to-display-image-with-loaddatawithbaseurl-method-in-android後? – user370305 2012-08-01 07:16:32

+0

是的,但這個問題與SD卡無關。 – 2012-08-01 07:17:25

+0

[在webview中加載保存在SD卡中的圖像]的可能的重複(http://stackoverflow.com/questions/5051364/load-the-image-saved-in-sdcard-in-webview) – Praveenkumar 2012-08-01 07:18:09

回答

3

您的加載語句中有語法錯誤。你必須把:file

myWebView.loadDataWithBaseURL("file:///mnt/sdcard/com.mypackage.myproject/3", myHtml, "text/html", "utf-8", ""); 
+0

是的,但在我的代碼中是「file:/// ...」。 – 2012-08-01 07:20:47

+0

這並不是我給出答案時。 – 2012-08-01 07:22:44

+0

我知道我知道我編輯,因爲你:) – 2012-08-01 07:24:56