我想使用"file:///"将本地html加载到WebView中,因为它不允许使用cookie.有没有办法使用像"localhost"这样的东西?
其次,我找不到在getSettings()中启用cookie的方法.因为使用"file:///"时不允许使用cookie.
处理完文件后,我得到一个HTML图像,其中图像设置为
<img src="abc.001.png" width="135" height="29" alt="" style="margin-left:0pt; margin-top:0pt; position:absolute; z-index:-65536" />
Run Code Online (Sandbox Code Playgroud)
不应修改图像的路径,因为我必须从列表中选择文件项.该图像与文件位于同一目录中.我使用loadData/loadDataWithBaseURL加载HTML字符串,但不显示图像.我只看到它的框架.
我怎样才能解决这个问题?我可以应用该解决方案,以防我有许多图像被索引为.001.jpg,.002.png等...(所有在目录中)?
更新:谢谢,无论我如何命名图像,它都适用于loadUrl()语句.事实上,我必须在将内容加载到WebView之前阅读并处理内容.这就是我使用loadDataWithBaseUrl()语句并解决上述问题的原因.这是我在测试项目中的代码,用于读取和显示Test.html的内容.
String res = "";
File file = new File(Environment.getExternalStorageDirectory()+"/Test.html");
try {
FileInputStream in = new FileInputStream(file);
if (in != null) {
BufferedReader buffreader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = buffreader.readLine()) != null) {
res += line;
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
wv.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
//wv.loadUrl("file://"+Environment.getExternalStorageDirectory()+"/Test.html");
Run Code Online (Sandbox Code Playgroud)
//中的语句有效,但这不是我在实际项目中可以做的.我有一个解决方案:处理完内容后,我必须将其保存在一个临时的HTML文件中然后加载它,该文件将在以后删除.但是,我仍然期待更好的解决方案:)