问题
I have few image in asset folder! I want to use these image path for img tag in html! I use below code for get path but it doesn't work.
String p="file:///android_asset/img.jpg";
what can I do?
this is my code
String p="file:///android_asset/img.jpg";
String html = "<html dir=\"rtl\" lang=\"fa-IR\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1\"></head><body class=\"rtl single single-post postid-38 single-format-standard custom-background\"></h3><center>MY TEXT<br><img src="+p+" /><hr>FOOTER<hr></center></body></html>";
my problem is src=p , because this app can't load image!
回答1:
Try this way its working for me
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String sHtmlTemplate = "<html><head></head><body><img src=\"file:///android_asset/ic_launcher.png\"><p>Hello Webview.</p></body></html>";
WebView wb = new WebView(this);
wb.loadDataWithBaseURL(null, sHtmlTemplate, "text/html", "utf-8",null);
setContentView(wb);
}
}
Output:
回答2:
this is my code
String p="file:///android_asset/img.jpg";
String html = "<html dir=\"rtl\" lang=\"fa-IR\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=1\"></head><body class=\"rtl single single-post postid-38 single-format-standard custom-background\"></h3><center>MY TEXT<br><img src="+p+" /><hr>FOOTER<hr></center></body></html>";
my problem is src=p , because this app can't load image!
回答3:
You need to assign the same string to src
attribute in img
tag like :
<img src="file:///android_asset/img.jpg" ... >
If it is in a variable assing it as :
<img src="+filename+"... >
I don't have a html file & i make html and load it from string at runtime
String filename ="file:///android_asset/img.jpg";
String htmlString= "<html><img src="+ filename +" height='100%' width='100%'></html>"; // whatever is your html text
wb.loadUrl(htmlString);
For more information, refer this
来源:https://stackoverflow.com/questions/26424717/android-how-to-using-image-in-asset-folder-in-html-to-load-in-webview