In meinem Projekt habe ich eine Datei:
"MyProject/assets/folder1/image1.jpg"
"MyProject/assets/folder1/index.html".
In WebView muss ich index.html (mit Bildern) öffnen.
Ich versuche diesen Code:
String baseUrl = "file:///Android_asset/folder1/";
webView.loadDataWithBaseURL(baseUrl, readFileAsString("index.html") , mimeType, "UTF-8", null);
Bilder werden jedoch nicht geladen.
Wenn ich Bilder in das Verzeichnis " assets " (MyProject/assets/
) lege und baseUrl = "file:///Android_asset"
herstelle, werden die Bilder korrekt geladen.
Wie lade ich Bilder nicht nur aus dem Stammverzeichnis, sondern auch aus assets/folder1
?
versuchen Sie es so
WebView webview = (WebView)this.findViewById(R.id.webview);
String html = "<html><head><title>TITLE!!!</title></head>";
html += "<body><h1>Image?</h1><img src=\"icon.png\" /></body></html>";
webview.loadDataWithBaseURL("file:///Android_res/drawable/", html, "text/html", "UTF-8", null);
Für weitere Informationen versuchen Sie diesen link
perfect LoadDataWithBaseurl
Ich denke, Sie müssen die Basis auf Assets setzen und die Unterordner wie folgt zu Ihren Image-SRCs hinzufügen:
webView.loadDataWithBaseURL("file:///Android_asset/", readAssetFileAsString("folder1/index.html"), "text/html", "UTF-8", null);
Html: <img src="folder1/image1.jpg">
Das funktionierte für mich unter Android 5.1
private String readAssetFileAsString(String sourceHtmlLocation)
{
InputStream is;
try
{
is = getContext().getAssets().open(sourceHtmlLocation);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");
}
catch(IOException e)
{
e.printStackTrace();
}
return "";
}
versuchen Sie das zu mögen
try {
String filePath = null;
filePath = "Your File path";
Bitmap bitmap = null;
bitmap = BitmapFactory.decodeFile(filePath);
Log.v("Image data-->", "" + bitmap);
imageWidth = bitmap.getWidth();
imageHeight = bitmap.getHeight();
Log.e("Width", "" + imageWidth);
filePath = "file://" + filePath;
String html = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><meta http-equiv=\"Content-Type\" content=\"text/html\";charset=utf-8\"/><title></title></head><body style=\"width:"
+ imageWidth
+ "px; height:"
+ imageHeight
+ "px; background:url("
+ filePath
+ ") no-repeat; position:relative;\">"
+ getDivTag(mapList)
+ "</body></html>";
Log.v("MIS", "" + html);
webview.getSettings().setSupportZoom(true);
webview.loadDataWithBaseURL(null, html, "text/html", "utf-8", null);
System.out.println(html);
} catch (Exception e) {
e.printStackTrace();
}