-4

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!

SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74

3 Answers3

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:

enter image description here

Maveňツ
  • 1
  • 12
  • 50
  • 89
  • have been error! force close! what can i do? – kazem fallahi Oct 17 '14 at 13:49
  • Hi @Gattsu, I am forced to define my src path as "/images/image.png", because html is dynamic in my case(coming from server). I have placed my image.png in images folder under assets but it s not showing up the image. – Mansuu.... Nov 22 '17 at 09:38
0

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

Community
  • 1
  • 1
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
0

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!