使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)


問題描述

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

I would like to set some html content in my webview with some pictures. I've read on the internet i must use loadDataWithBaseUrl to do such a thing because i need to link the folder of images (basUrl) in order.

My html content nicely loads, i can even ran the javascripts perfectly, but for some reason my images cannot be loaded.

Somewhere i've read there are some security reasons and thats why i cant load images from sd card to webview and some says it can be easily done via loadDataWithBaseUrl, so i really dont know which one is true.

Here is my method i tried, maybe with some mistakes so dont be rude:

I got my html file here:

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

My images are here:

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

And this is my content loading:

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

In my html code:

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

So as you see i give the baseUrl, and for some reason the webview cannot load my images.

As many said, this can be a solution:

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");

BUT, i have 700 different html files and there are many images in many different places... so i cannot modify the html code.

Is there a way to link the image folder to my html files to properly use them as a baseUrl?


參考解法

方法 1:

You have a syntax error in your loading statement. You have to put : after file

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

方法 2:

You don't need to put full path of your image  into html. 

img src="images/test1.jpg"

Instead

img src=\""+ imagePath + "\"

(by Adam VarhegyiIñigoShahzad Naseer)

參考文件

  1. Using webView's loadDataWithBaseURL not loading images from sdcard (CC BY-SA 3.0/4.0)

#android-webview #url #Android #android-sdcard






相關問題

如何在我創建的 Android WebView 中播放動態 mp4 視頻 (How do I play dynamic mp4 video within Android WebView I created)

Phonegap 應用程序不滾動 (Phonegap Application Not Scrolling)

Android - 將本地圖像設置為 WebView 中的 img 元素 (Android - Set a Local Image to an img element in WebView)

使用 webView 的 loadDataWithBaseURL 不從 sdcard 加載圖像 (Using webView's loadDataWithBaseURL not loading images from sdcard)

WebView LoadURL dan GetView (WebView LoadURL and GetView)

Android WebView 在 android 4.0 + 上崩潰應用程序 (Android WebView crashes application on android 4.0 +)

Android: Chế độ xem hình ảnh chụm-thu phóng với Chế độ xem con được nối thêm (Android: Pinch-zooming ImageView with appended child Views)

android.webkit.WebView 無法轉換為 android.widget.Button (android.webkit.WebView cannot be cast to android.widget.Button)

使用 android studio 在 webview 中顯示谷歌表單 (display google forms in webview using android studio)

如何解決使用 WebView 時出現“無法打開數據庫文件”的錯誤? (How to solve error "unable to open database file" when using WebView?)

android webview 顯示其字體的默認字體系列是什麼? (What is the default font family taken by android webview to show its fonts?)

Android Studio - 視頻音量低 (Android Studio - Video Volume Low)







留言討論