加载图像使用的AsyncTask [英] Loading Image using AsyncTask

查看:195
本文介绍了加载图像使用的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自定义列表适配器它具有图像的每一个它需要从互联网上下载的项目。当我第一次进入活动 - 应用程序冻结了一会儿,直到图像下载,然后在活动列表加载。

正如我可以做出来的图像正在活动的加载之前下载。我怎样才能下载图像的活动已经加载之后。我想我需要使用异步任务。但自从我加载在自定义数组适配器不知道照片怎么办呢。

这是我的自定义适配器的 getView()

 公开查看getView(INT位置,查看convertView,ViewGroup中父){
        视图V = convertView;

        如果(V == NULL){
            LayoutInflater六;
            VI = LayoutInflater.from(的getContext());
            V = vi.inflate(R.layout.list_item_default,NULL);
        }

        项目P = items.get(位置);

        如果(P!= NULL){

            TextView的list_title =(TextView中)v.findViewById(R.id.list_title);
            TextView的list_description =(TextView中)V
                    .findViewById(R.id.list_description);
            TextView的list_timestamp =(TextView中)V
                    .findViewById(R.id.list_timestamp);
            ImageView的list_image =(ImageView的)v.findViewById(R.id.list_image);

            如果(list_title!= NULL){
                list_title.setText(p.getItemTitle());
            }

            如果(list_description!= NULL){
                list_description.setText(p.getItemDescription());
            }

            如果(list_timestamp!= NULL){
                list_timestamp.setText(p.getItemTimestamp());
            }

            如果(list_image!= NULL){
                Log.d(bMobile,里面的getView()图像);
                尝试 {
                    URL IMAGEURL =新的URL(p.getItemImage());
                    HttpURLConnection的CON =(HttpURLConnection类)IMAGEURL
                            .openConnection();
                    InputStream的inputStrem = con.getInputStream();
                    位图图像= BitmapFactory.de codeStream(inputStrem);
                    如果(NULL!=图)
                        list_image.setImageBitmap(图像);
                    其他
                        Log.d(bMobile,位图是空);
                }赶上(例外五){
                }
            }
        }

        返回伏;
    }
 

AsyncTask的:

 公共类DownloadImagesTask扩展的AsyncTask< ImageView的,无效的,位图> {

    ImageView的ImageView的= NULL;

    @覆盖
    受保护的位图doInBackground(ImageView的... imageViews){
        this.imageView = imageViews [0];
        返回download_Image((字符串)imageView.getTag());
    }

    @覆盖
    保护无效onPostExecute(位图的结果){
        imageView.setImageBitmap(结果);
    }

}
    私人位图download_Image(字符串URL){

        BMP位= NULL;
        尝试{
            URL ulrn =新的网址(URL);
            HttpURLConnection的CON =(HttpURLConnection类)ulrn.openConnection();
            InputStream的是= con.getInputStream();
            BMP = BitmapFactory.de codeStream(是);
            如果(NULL!= BMP)
                返回BMP;

            }赶上(例外五){}
        返回BMP;
    }
 

解决方案

应用程序冻结 - 这是因为你正在下载的事件线程或UI线程上的图像。你不应该这样做。所有阻塞操作,长期运行的操作,网络操作,应该在一个单独的线程中运行。是的,你可以使用的AsyncTask来做这应该可以解决这个问题的图像加载。

两个选项,你可以做些什么来解决这个问题。

  1. 使用WebImageView从在<一个机器人福库href="http://brainflush.word$p$pss.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/">http://brainflush.word$p$pss.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ WebImageView使用缓存,以及所以你不需要重新下载图像您已经下载了这些。下载图像是昂贵的,消耗的数据。 我用WebImageView,并能在列表加载图像。我只花了约20分钟,以得到这一切的工作,让你知道它很容易整合它。

  2. 第二个选择是使用异步任务。请检查<一href="http://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask">Android :从Web与AsyncTask的加载图像。有计算器中的关于如何做到这一点更多的信息。

任何时候,你的UI挂起,你应该有一个毛骨悚然的感觉,你正在做的事情在UI线程上。

I am trying to create a custom List Adapter which has an Image for every item it needs to download from the internet. When I first enter the Activity - the app freezes for a while till the images are downloaded and then the Activity List is loaded.

As I can make out the images are being downloaded before the activity loads. How can I download the images after the activity has loaded. I think I need to use Async Task. But since I am loading the images in the Custom Array Adapter not sure how to do it.

This is my Custom Adapter's getView():

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_item_default, null);
        }

        Item p = items.get(position);

        if (p != null) {

            TextView list_title = (TextView) v.findViewById(R.id.list_title);
            TextView list_description = (TextView) v
                    .findViewById(R.id.list_description);
            TextView list_timestamp = (TextView) v
                    .findViewById(R.id.list_timestamp);
            ImageView list_image = (ImageView) v.findViewById(R.id.list_image);

            if (list_title != null) {
                list_title.setText(p.getItemTitle());
            }

            if (list_description != null) {
                list_description.setText(p.getItemDescription());
            }

            if (list_timestamp != null) {
                list_timestamp.setText(p.getItemTimestamp());
            }

            if (list_image != null) {
                Log.d("bMobile", "inside getView() image");
                try {
                    URL imageURL = new URL(p.getItemImage());
                    HttpURLConnection con = (HttpURLConnection) imageURL
                            .openConnection();
                    InputStream inputStrem = con.getInputStream();
                    Bitmap image = BitmapFactory.decodeStream(inputStrem);
                    if (null != image)
                        list_image.setImageBitmap(image);
                    else
                        Log.d("bMobile", "Bitmap is Null");
                } catch (Exception e) {
                }
            }
        }

        return v;
    }

AsyncTask:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

}
    private Bitmap download_Image(String url) {

        Bitmap bmp =null;
        try{
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)
                return bmp;

            }catch(Exception e){}
        return bmp;
    }

解决方案

"the app freezes" - This is because you are downloading the images on the event thread or UI Thread. You should never do that. All blocking operations,long running operations, network operations, should be run on a separate thread. Yes you can use asynctask to do the image loading which should fix the issue.

Two options you can do to solve the issue is

  1. Use WebImageView from droid fu library at http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ WebImageView uses caching as well so you dont need to download images again which you have already downloaded. Downloading images are expensive and consume data. I used WebImageView and was able to load images in list. It took me just about 20 mins to get it all working, so you know its easy integrating it.

  2. Second option is to use async task . Please check Android : Loading an image from the Web with Asynctask . There are lot more info in stackoverflow regarding how to do this.

Any time your UI hangs, you should have a creepy feeling, that you are doing something on the UI thread.

这篇关于加载图像使用的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆