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.
Asked
Active
Viewed 1,014 times
2 Answers
1
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
-
The image path are like http://upload/student/image1.png. I just want the error msg(like web page not available) is not show in webview instead of that load default image(like blank image) of student. – Kailas Nov 13 '13 at 12:58
-
Yout path has to be imagePath = "file://"+ base + "/upload/student/image1.png"; I don't understand what you want. Is it to have something show up if there is nothing to show ? – azerto00 Nov 13 '13 at 13:01
-
If there is nothing to show then show default image. The images are not located in device the image place on the server. – Kailas Nov 13 '13 at 13:04
1
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.
-
There is also https://github.com/nostra13/Android-Universal-Image-Loader which is a good and simple library – azerto00 Nov 13 '13 at 13:23
-
VOLLEY is more efficent and usefull as it was disscussed in GOOGLE I/O and for the first time it might looks difficult to implement but when you use it for once it will never be an issue to do so ... – Jamal Nov 13 '13 at 13:34