I have several images on some of my Activities, and these are all connected from Remote Server. The problem is every time I go on one of these Activities that contain images from remote sever. All of the images must load first otherwise you will get a black empty screen. Sometimes it takes about 1-5 minutes to load, and sometimes it even force close the device. Is there anyway I can fix this issue? Some people suggest me to use Thread, but I really don't know how make one. Can some give me someone example, I would really appreciate a lot.
开发者_Go百科Here is the code I'm using:
ImageViewimgView =(ImageView)findViewById(R.id.image01);
Drawable drawable = LoadImageFromWebOperations("http://forum.roda.hr/images/customavatars/avatar10164_2.gif");
imgView.setBackgroundDrawable(drawable);
private Drawable LoadImageFromWebOperations(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;
}
}
I recommend the DroidFu library for this. It has a WebImageView widget, which will handle the loading of images from a remote source. https://github.com/kaeppler/droid-fu
Try this example, which uses AsyncTask and ProgressDialog to perform work in background and to show user that work is in progress.
精彩评论