Kailas
Kailas

Reputation: 3231

Image load in webview or not?

I'm using a webview to show image. I used the webview.loadUrl("imagepath"); these code to show image but the webview failed to load the image then i want to show my default image in the webview.

Upvotes: 0

Views: 1017

Answers (2)

Jamal
Jamal

Reputation: 11

It is not a good practice to load image in a webview.

The solution is to use some kind of image loader and the best one for your case is VOLLEY. It provides cache and image loading functionality better then any other image loader.

Here is the link.

Upvotes: 1

azerto00
azerto00

Reputation: 1000

You have to add file:/// before your image Path.

Did you try with something like

// Exemple from asset folder
webview.loadUrl("file:///android_asset/mu_image.jpg");
 // From external storage
String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
String imagePath = "file://"+ base + "/YOUR_FILE.jpg";
webview.loadUrl(base);

EDIT (after understanding the question ...)

You have to check if your web file exist

public static boolean exists(String URLName){
try {
  HttpURLConnection.setFollowRedirects(false);
  // note : you may also need
  //        HttpURLConnection.setInstanceFollowRedirects(false)
  HttpURLConnection con =
     (HttpURLConnection) new URL(URLName).openConnection();
  con.setRequestMethod("HEAD");
  return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
   e.printStackTrace();
   return false;
}
  }

Then 2 choices : - The file exists, show it - Or, show your default image

Source : Check if file exists on remote server using its URL

Upvotes: 1

Related Questions