Android:图像下载问题

时间:2012-04-13 11:40:38

标签: android

我有一个应用程序,我正在下载并在一个线程中显示图像。一切都很好。但是当图像被加载时,相同的图像被加载到两到三个位置,当我们滚动屏幕时,图像被正确加载。你们中的任何一个人都可以对此提出任何建议吗?

代码:

try{
    holder.progress.setVisibility(View.VISIBLE);
    DownLoadImageInAThreadHandler(Item, holder);

}
catch(Exception e)
{
    System.out.println("Exception in Downloading image : " + e.getMessage());
}
return convertView;
}


//This code is outside the getview method
public void DownLoadImageInAThreadHandler(final CategoryData Item, final ViewHolder holder)
{
    nImageDownLoads++;

    System.out.println("The images being downloaded :" + nImageDownLoads);

    final Handler handler = new Handler()
    {
        @Override  public void handleMessage(Message message)
        {
            holder.imgitem.setImageDrawable((Drawable) message.obj);
            holder.imgitem.setVisibility(View.VISIBLE);
            holder.progress.setVisibility(View.GONE);
        }
    };
    //Thread for downloading the images
    Thread t = new Thread()
    {
        public void run()
        {
            try
            {
                Item.bImageDownLoaded = 2;
                System.out.println("Downloading image      :"+Item.ImageUrl);
                drawable=getDrawableFromUrl(Item.ImageUrl);
                nImageDownLoads--;
                System.out.println("Downloaded image :" + Item.ImageUrl);
                System.out.println("Remaining images for downloading: " + nImageDownLoads);
                if(drawable != null)
                {                                                          
                    Item.bImageDownLoaded = 1;
                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, drawable);
                    handler.sendMessage(message);
                }
                else{
                    int idNoImage = R.drawable.giftsuggestionsnoimage;
                    Drawable dwgNoImg = getParent().getResources().getDrawable(idNoImage);

                    //Send the message to the handler
                    Message message = handler.obtainMessage(1, dwgNoImg);
                    handler.sendMessage(message);
                }
            }
            catch(Exception exp)
            {
                System.out.println("Exception in DownLoadImageInAThread : " + exp.getMessage());
            }
        }

        private Drawable getDrawableFromUrl(String imageUrl) throws IOException
        {
            try
            {
                image = DownloadDrawable(imageUrl, "src");
                //bmp = readBitmapFromNetwork(new URL(imageUrl));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            return image;
            //return bmp;
        }
    };
    t.start();
}

Drawable DownloadDrawable(String url, String src_name) throws java.io.IOException
{
    return Drawable.createFromStream(((java.io.InputStream) new java.net.URL(url).getContent()),  src_name);
}

2 个答案:

答案 0 :(得分:0)

您使用的是ListView吗?如果是这样,那么在您将图像写入其中时,ViewView是否已经可以回收(重复用于显示另一个列表项)?

答案 1 :(得分:0)

问题发生是因为您已编写代码以getView()方法加载图像。

每次滚动列表时都会调用该方法。

所以这根本不是一个好主意。

从那里删除该代码.. 加载图片以及其他threadAsyncTask 然后致电notifyDataSetChanged()对象Adapter class

只需按照我的指示进行操作,请仔细阅读并尝试实施,如果无法理解任何事情,请随时询问..无法粘贴任何代码段。

  

ListView适配器的getView()方法

show default icon if array doesn't contain bitmap at that position
if bitmap is there show bitmap else show default icon

AsyncTask to Load Data

do in background
load data and show in listview

AsyncTask to Load Images

do in background
download images one bye one from the urls got in first AsyncTask
in onPostExecute call adapter's notifyDataSetChanged() to show the bitmaps too

you can also call onProgressUpdate periodically means..after 2-3 image download 
call publishProgress in doInBackground which will call onProgressUpdate and 
in that method also write adpater.notifyDataSetChanged() to 
reflect currently downloaded/available bitmaps in array