Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Url to a WebView

I am trying to set an image to a WebView for my app.

I am using the below code to set the image to my WebView,

  String imageUrl =  " file:///android_res/drawable/dinner_menu.png";
    WebView wv = (WebView) findViewById(R.id.yourwebview);
    wv.getSettings().setBuiltInZoomControls(true);
   wv.loadUrl(imageUrl);

This works fine for Android 2.2 and higher.

But if I try to run the code in 2.1 or lower, it shows an error as,

The requested file was not found /android_res/drawable/dinner_menu.png.

Can anyone help me out.

like image 902
Andro Selva Avatar asked Jun 17 '11 07:06

Andro Selva


1 Answers

Try putting your image in your assets folder, and load it using:

WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/dinner_menu.png");
setContentView(webView);

It should work on earlier versions of Android, but you'll need to manually manage using different versions of your image for different screen sizes / densities.

like image 89
Mark Allison Avatar answered Oct 09 '22 00:10

Mark Allison