gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:15:53 26 4
gpt4 key购买 nike

我正在使用 jsoup 来解析 html 和 webview 来显示 html 内容。在此过程中,我正在更改图像的来源。所以当在 html 中执行这行时 <img src="sdcard/rreadyreckoner_images/download-r.png" alt="downloadr" width="191" height="129" />
图像未加载,即图像还有剩余空间,但不包含实际图像。我怎样才能在android中实现这个?这是我正在使用的代码。

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

}
}

这是我试图在 WebView 中显示的 html。

<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>

我得到以下错误

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

最佳答案

你必须使用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", "");

关于android - 如何在android中使用html代码打开本地存储的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39019828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com