【发布时间】:2012-08-01 07:14:47
【问题描述】:
我想在我的 webview 中设置一些带有一些图片的 html 内容。 我在网上看到我必须使用 loadDataWithBaseUrl 来做这样的事情,因为我需要按顺序链接图像文件夹 (basUrl)。
我的 html 内容加载得很好,我什至可以完美地运行 javascript,但由于某种原因 我的图片无法加载。
在某处我读到有一些安全原因,这就是为什么我不能将图像从 sd 卡加载到 webview 并且有人说它可以很容易地完成通过 loadDataWithBaseUrl,所以我真的不知道是哪一个是真的。
这是我尝试的方法,可能有一些错误,所以不要粗鲁:
我的 html 文件在这里:
mnt/sdcard/com.mypackage.myproject/3/3.html
我的图片在这里:
mnt/sdcard/com.mypackage.myproject/3/images/Cover.png
这是我的内容加载:
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?
【问题讨论】:
-
是的,但这个问题与sdcard无关。
-
如果你通过评论也正确,那么这个问题肯定会帮助你。
-
你必须使用 File Uri 在 WebView 中加载图像。
标签: android url android-webview android-sdcard