Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在webview中加载SD卡中保存的图像_Android_Image_Webview - Fatal编程技术网

Android 在webview中加载SD卡中保存的图像

Android 在webview中加载SD卡中保存的图像,android,image,webview,Android,Image,Webview,使用以下代码 mWebView = (WebView) findViewById(R.id.webview); mWebView.getSettings().setAllowFileAccess(true); mWebView.getSettings().setJavaScriptEnabled(true); String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); String i

使用以下代码

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().SetBuilTinZoomControl(true);
String base=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
字符串imagePath=base+“/test.jpg”;
字符串html=“”;
loadData(html,“text/html”,“utf-8”);

请帮助

网络视图可以显示HTML而不是图像。您或者需要使用ImageView,或者生成一些带有图像标记的HTML来显示您的图片。如果需要动态,可以将其生成为字符串,并使用loadData()方法显示它

编辑:在html字符串中需要类似的内容

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");
String base=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
字符串imagePath=base+“/test.jpg”;
字符串html=(“
"); 
loadData(html,“text/html”,“utf-8”);
mWebView=(WebView)findViewById(R.id.WebView);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().SetBuilTinZoomControl(true);
String base=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
字符串imagePath=“file://“+base+”/test.jpg”;
字符串html=“”;
loadDataWithBaseURL(“,html,“text/html”,“utf-8”,”);

这就成功了,因为我们必须在任何文件之前附加“prefix”file://”,以便在webview中显示

我尝试了前面提到的方法,但没有成功,因此这些选项对我从外部存储器加载图像很有效:

将图像直接加载到网络视图中。

 String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
 String imagePath = pathExternalStorage + "/" + "image.jpg";

String imagePathWV = "file://" + imagePath;
String html = ("<html><head></head><body><img src=\""+ imagePathWV + "\"></body></html>");
webView.loadDataWithBaseURL(null, html, "text/html","utf-8",null);
假设我在外部存储目录的根目录中有一个名为
image.jpg
的映像(在我的例子中是
/storage/emulated/0/image.jpg

使用html模板加载图像以加载到Web视图。

 String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
 String imagePath = pathExternalStorage + "/" + "image.jpg";

String imagePathWV = "file://" + imagePath;
String html = ("<html><head></head><body><img src=\""+ imagePathWV + "\"></body></html>");
webView.loadDataWithBaseURL(null, html, "text/html","utf-8",null);
String path externalstorage=Environment.getExternalStorageDirectory().getAbsolutePath().toString();
字符串imagePath=pathExternalStorage+“/”+“image.jpg”;
字符串imagePathWV=“file://”+imagePath;
字符串html=(“”);
loadDataWithBaseURL(null,html,“text/html”,“utf-8”,null);

您将HTML字符串留空。我将编辑我的答案,稍后显示一个示例。您确定图像的路径构建正确吗?在调用loadData()之前,请尝试记录imagePath字符串并确保图像位于其在日志中显示的位置。同时尝试将一些纯文本添加到元素内的html字符串中,以查看是否显示。如果文本显示但图片未显示,则文件路径有问题。@Tim i将查找。..image的权限为--rwxr-x….如果添加了一些文本,则很容易显示。@Tim stringhtml=和字符串imagePath=/mnt/sdcard/test.jpglogged@Tim最后,我能够解决我们必须在基本路径之前附加file:,以将图像加载到WebView。当我插入
mWebView.loadDataWithBaseURL(“,content,“text/html”,“utf-8”,”);
而不是
mWebView.loadData(html,“text/html”)时,它对我起到了作用“,“utf-8”)它不工作。。我们已经像这样替换了url,并使用mWebView.loadDataWithBaseURL(“”,html,“text/html”,“utf-8“,”);谢谢你,它像一个符咒!在我的html“”中插入mWebView.loadDataWithBaseURL(“”,内容,“text/html”,“utf-8”和“”)对我来说也很有用;而不是mWebView.loadData(html、“text/html”、“utf-8”);在阅读了答案之后,我仍然错过了loadDataWithBaseURL。不确定loadData与此之间有什么不同。无论如何,谢谢你
 String pathExternalStorage = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
 String imagePath = pathExternalStorage + "/" + "image.jpg";

String imagePathWV = "file://" + imagePath;
String html = ("<html><head></head><body><img src=\""+ imagePathWV + "\"></body></html>");
webView.loadDataWithBaseURL(null, html, "text/html","utf-8",null);