开发者

How to use thread to load images from server?

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜