在 webview 中加载保存在 sdcard 中的图像 [英] Load the image saved in sdcard in webview

查看:29
本文介绍了在 webview 中加载保存在 sdcard 中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码

mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = base + "/test.jpg";  
mWebView.loadUrl(imagePath);

图片没有加载...

也试过

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 = base + "/test.jpg";
String html = "<html><head></head><body><img src=""+ imagePath + ""></body></html>";
mWebView.loadData(html, "text/html","utf-8");

请帮忙

推荐答案

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.loadDataWithBaseURL("", html, "text/html","utf-8", "");  

这很有效,因为我们必须在任何文件之前附加前缀"file://,以便在 webview 中显示

This did the trick as we have to append the"prefix "file://" before any file so as to display in the webview

这篇关于在 webview 中加载保存在 sdcard 中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆