如何在android中使用html代码打开本地存储的图像?

时间:2016-08-18 13:43:41

标签: android html image jsoup

我使用jsoup解析html和webview来显示html内容。在这个过程中,我正在改变图像的来源。因此,当在html中执行此行时io_service::run()
图像没有加载,即图像有剩余空间,但不包含实际图像。我怎样才能在android中实现这一点? 这是我正在使用的代码。

<img src="sdcard/rreadyreckoner_images/download-r.png" alt="downloadr" width="191" height="129" />

这是我试图在网页视图中显示的HTML。

    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();

        }
    }

我收到了以下错误

  

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

1 个答案:

答案 0 :(得分:3)

你必须使用Environment.getExternalStorageDirectory()来获得SD卡的真实路径:

 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";
//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", "");