0

I load a .jpg in a WebView. My problem is that I found this: Android WebView, Scaling Image to fit the screen

and it doesn't work for me.

Here is my code:

Display display = getWindowManager().getDefaultDisplay();
    int width= display.getWidth();

    Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


    String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
    html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);
Community
  • 1
  • 1
Billabong
  • 457
  • 2
  • 8
  • 23

3 Answers3

5

This wasn't working for me as i had high resolution phone.

Try this, it worked for me, webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

Max
  • 1,528
  • 21
  • 33
4

You can style img tag before you load HTML into web view. Please see below code

WebView content = (WebView) findViewById(R.id.webView1);
String head = "<head> <style>img{display: inline;height: auto;max-width:   100%;}</style> <style>body {font-family: 'Roboto';  }</style></head>";

content.loadDataWithBaseURL(null, head + post.getContent(), "text/html", "UTF-8", null);
BB_Dev
  • 461
  • 1
  • 5
  • 11
2

you are html image tag wrong check below code:

Display display = getWindowManager().getDefaultDisplay();
int width= display.getWidth();

Toast.makeText(getApplicationContext(), ""+width, Toast.LENGTH_LONG).show();


String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
   html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";

aboutText.loadDataWithBaseURL("file:///android_res/drawable/", html, "text/html","UTF-8" , null);

your html string wrong formatted image tag like below code:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\"<img src=\""+"image.jpg"+"\" /></body></html>";

formatted my code:

String html = "<html><head><title>Example</title><meta name=\"viewport\"\"content=\"width="+width+", initial-scale=0.65 \" /></head>";
html+= "<body><img width=\""+width+"\" src=\""+"image.jpg"+"\" /></body></html>";
Stack Overflow User
  • 4,052
  • 6
  • 29
  • 47