我想更改html内容中每个图片的来源。 源更改后,我想在webview中显示html内容。 这是我试图执行的代码。 此代码的问题是它将所有图像更改为最后解析的图像。
private void show_webview( String instruction ) {
ParseJSON pj = new ParseJSON(instruction);
pj.parseJSON();
String[] instructions = ParseJSON.instruction;
for( int i = 0; i < instructions.length; i++ ) {
instructions[i] = instructions[i].replaceAll("Â"," ");
Document doc = Jsoup.parse(instructions[i]);
Elements img = doc.getElementsByTag("img");
int no_imgs = img.size();
int im=0;
String src = null;
String img_src[] = new String[no_imgs];
for (Element el:img) {
src = el.absUrl("src");
downloadImages(src);
img_src[im] =src;
im++;
}
for (int x=0; x< img_src.length; x++) {
int indexname = img_src[x].lastIndexOf("/");
if (indexname == img_src[x].length()) {
img_src[x]= img_src[x].substring(x, indexname);
}
indexname = img_src[x].lastIndexOf("/");
String name = img_src[x].substring(indexname, img_src[x].length());
Log.d("Image name", name);
Element image1 = doc.select("img").first();
String img_url = image1.absUrl("src");
if (img_src[x].contains(name.toLowerCase())) {
Boolean isSDPresent =
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (isSDPresent) {
Elements image = doc.select("img[src]");
String imagePath = "file://"+Environment.getExternalStorageDirectory().getAbsolutePath() + "/rreadyreckoner_images" + name;
image.attr("src", imagePath);
Log.d("PATH", imagePath);
}
else {
Elements image = doc.select("img[src]");
String imagePath = "file://"+"/storage/emulated/0/rreadyreckoner_images"+name;
image.attr("src", imagePath);
}
}
}
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.loadDataWithBaseURL( "", String.valueOf(doc), "text/html", "utf-8", "" );
Log.d("Instruction",instructions[i]);
}
}