10ff
10ff

Reputation: 813

HTML WebView doesn't show background-image

I'm trying to implement a HTML file into a WebView. The problem is, that the images, including the backgroundimage, the HTML file refers to, aren't shown. But just in the emulator. The backgroundimage works in the browser very well.

I reduced it all to a minimal code, but it still doesn't work. The background-color works well, but all the backgroundimage isn't visible:

HTML-file Test.html:

<html>
<head>

</head>
<body background="ipad_bg.jpg" bgcolor="#5611cc">
Test
</body>
</html>

Activity:

package my.Package;

/*imports...*/

public class Ueber extends Activity {

WebView mWebView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ueber_layout);

    mWebView = (WebView) findViewById(R.id.ueber_webview);
    mWebView.getSettings().setJavaScriptEnabled(true);

    try {
        InputStream fin = getAssets().open("Test.html");
            byte[] buffer = new byte[fin.available()];
            fin.read(buffer);
            fin.close();
            mWebView.loadData(new String(buffer), "text/html", "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
    }        
    }
  }

ueber_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ueber_webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

The Test.html file is in the assets-folder, the image ipad_bg.jpg, too. So, the color is shown, but not the image.

Somebody has an idea?

Upvotes: 0

Views: 2646

Answers (1)

mudit
mudit

Reputation: 25536

Try opening your web page like this:

mWebView.loadUrl("file:///android_asset/Test.html");

I also have images in my html page and it is working fine for me.

Upvotes: 1

Related Questions