I have this code that pulls an image from an url and displays it within the app.
Drawable drawableTest = LoadImageFromWeb("http://www.someimageurl");
imgView.setImageDrawable(drawableTest);
private Drawable LoadImageFromWeb(String url){
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
The method works and the image is pulled into the app without a problem. What I’d like to do is two things…
1) Move the process to a background thread.
2) Display a “loading” drawable (located in drawable folder) while the image is being downloaded and then switch to image when done. If there’s a problem I’d like to load a “unable to load image” drawable (also located in drawable folder).
Can someone help modify the method above so that it will run in the background and display the appropriate images upon loading and if there’s a problem?
Thanks! You guys rock btw.
Shannon
You can use
AsyncTaskto run the task in another thread. That way, it won’t block UI. Roughly, it should be something like this:You can change the
ProgressDialogwith whatever you like…