如何在android中使用html代码打开本地存储的图像? [英] How to open a image stored locally using html code in android?

查看:98
本文介绍了如何在android中使用html代码打开本地存储的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jsoup解析html和webview来显示html内容。在这个过程中,我正在改变图像的来源。因此,当在html中执行此行时,< img src =sdcard / rreadyreckoner_images / download-r.pngalt =downloadrwidth =191height =129/> code>

图像不加载,即图像有剩余空间,但它不包含实际图像。我怎样才能在android中实现这一点?
这是我使用的代码。

  private void showJson(String json){
ParseJSON pj =新的ParseJSON(json);
pj.parseJSON();
String []指令= ParseJSON.instruction; (int i = 0; i< instruction.length; i ++)
{

Document doc = Jsoup.parse(instruction [一世]);
$ b $ if(doc.select(img [src])。attr(src)。contains(download-r.png))
{
元素image = doc.select(img [src]);
String imgsource = image.attr(src);
downloadImages(imgsource);
布尔isSDPresent = Environment.getExternalStorageState()。equals(Environment.MEDIA_MOUNTED);
if(isSDPresent)
{
image.attr(src,sdcard / rreadyreckoner_images / download-r.png);
} else
{
image.attr(src,/ storage / emulated / 0 / rreadyreckoner_images / download-r.png);


Log.d(Clean,String.valueOf(doc));
// String imgsrc = images.attr(src);

webView.getSettings()。setJavaScriptEnabled(true);
webView.loadData(String.valueOf(doc),text / html,UTF-8);


元素段落= doc.select(ul li); (Element element:段落)

{
Log.d(PARAGRAPHS,element.text());

}



//Log.d(\"SOURCE\",imgsrc);
//Toast.makeText(MainActivity.this,imgsrc,Toast.LENGTH_SHORT).show();


$ b $ / code $ / pre
$ b $ p这是我的html试图在webview中显示。

 < html> 
< head>< / head>
< body>
< ul>
<综合R档案网络< / li>
< li< li>全球网络服务器网络,其存储与R< / li>相同,最新版本的< br /
< / ul>
< p>< br />< strong>下载并安装R:< / strong>< / p>
< ul>
< li>使用离您最近的CRAN镜像,以更快的< br />速度下载R设置。转到< a href =url> HTTP://cran.r-project.org< / A>< /锂>
< li>根据您的机器选择三种下载链接之一。< / li>
< / ul>
< img src =sdcard / rreadyreckoner_images / download-r.pngalt =downloadrwidth =191height =129/>
< ul>
< li>运行R设置并按照安装程序的说明操作。< / li>
< / ul>
< / body>
< / html>

我收到以下错误消息:


chromium:[INFO:CONSOLE(1)]不允许加载本地资源:
file:///sdcard/rreadyreckoner_images/download-r.png


解决方案

您必须使用 Environment.getExternalStorageDirectory()获取sdcard的真实路径:

  Boolean isSDPresent = Environment.getExternalStorageState()。equals(Environment.MEDIA_MOUNTED); 
if(isSDPresent)
{
image.attr(src,Environment.getExternalStorageDirectory()+/rreadyreckoner_images/download-r.png);
} else
{
image.attr(src,/ storage / emulated / 0 / rreadyreckoner_images / download-r.png);
}



我建议将图像加载到您的WebView中:



  myWebView =(WebView)findViewById(R.id.webview); 
myWebView.getSettings()。setAllowFileAccess(true);
myWebView.getSettings()。setJavaScriptEnabled(true);
myWebView.getSettings()。setBuiltInZoomControls(true);

String imagePath =file://+ Environment.getExternalStorageDirectory()。getAbsolutePath()+rreadyreckoner_images / download-r.png;
//创建一个html模板。
String html =< html>< head>< / head>< body>< img src = \+ imagePath +\>< / body>< / HTML>中;
//将图像加载到WebView中:
mWebView.loadDataWithBaseURL(,html,text / html,utf-8,);


I am using jsoup to parse html and webview to display html content. In the process I am changing the source for a image. So when in html this line is executed <img src="sdcard/rreadyreckoner_images/download-r.png" alt="downloadr" width="191" height="129" />
the image doesn't load i.e. the there is space left for the image but it does not contain the actual image. How can I achieve this in android? This is the code I am using.

    private void showJson(String json) {
        ParseJSON pj = new ParseJSON(json);
        pj.parseJSON();
        String[] instruction = ParseJSON.instruction;


        for (int i = 0; i < instruction.length; i++)
        {

            Document doc = Jsoup.parse(instruction[i]);

            if (doc.select("img[src]").attr("src").contains("download-r.png"))
            {
                Elements image = doc.select("img[src]");
                String imgsource = image.attr("src");
                downloadImages(imgsource);
                Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
                if (isSDPresent)
                {
                    image.attr("src","sdcard/rreadyreckoner_images/download-r.png");
                }else
                {
                    image.attr("src","/storage/emulated/0/rreadyreckoner_images/download-r.png");
                }
            }
            Log.d("Clean", String.valueOf(doc));
//            String imgsrc = images.attr("src");

            webView.getSettings().setJavaScriptEnabled(true);
            webView.loadData(String.valueOf(doc),"text/html","UTF-8");


            Elements paragraphs = doc.select("ul li ");
            for (Element element: paragraphs)
            {
                Log.d("PARAGRAPHS",element.text());

            }



            //Log.d("SOURCE",imgsrc);
            //Toast.makeText(MainActivity.this, imgsrc, Toast.LENGTH_SHORT).show();

        }
    }

This is the html that I am trying to display in a webview.

<html>
   <head></head>
   <body>
      <ul>
         <li>The Comprehensive R Archive Network</li>
         <li>A network of global web servers storing identical, up-to-date, versions of<br />code and documentation for R</li>
      </ul>
      <p><br /><strong>Download and Install R:</strong></p>
      <ul>
         <li>Use the CRAN mirror nearest to you to download R setup at a faster<br />speed. Go to <a href="url"> http://cran.r-project.org</a></li>
         <li>Select one of the three download links according to your machine.</li>
      </ul>
      <img src="sdcard/rreadyreckoner_images/download-r.png" alt="downloadr" width="191" height="129" /> 
      <ul>
         <li>Run the R set up and follow the instructions of the installer.</li>
      </ul>
   </body>
</html>

I get the follwoing error

chromium: [INFO:CONSOLE(1)] "Not allowed to load local resource: file:///sdcard/rreadyreckoner_images/download-r.png"

解决方案

you have to use Environment.getExternalStorageDirectory() to get the real path of the sdcard:

 Boolean isSDPresent = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
                if (isSDPresent)
                {
                    image.attr("src", Environment.getExternalStorageDirectory() + "/rreadyreckoner_images/download-r.png");
                }else
                {
                    image.attr("src","/storage/emulated/0/rreadyreckoner_images/download-r.png");
                }

I suggest to load the image into your WebView this method:

myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);

String imagePath = "file://"+ Environment.getExternalStorageDirectory().getAbsolutePath() + "rreadyreckoner_images/download-r.png";
//Create an html template.
String html = "<html><head></head><body><img src=\""+ imagePath + "\"></body></html>";
//Load the image into your WebView:
mWebView.loadDataWithBaseURL("", html, "text/html","utf-8", "");  

这篇关于如何在android中使用html代码打开本地存储的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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