图像再次下载&再次

时间:2014-05-27 05:41:25

标签: android bitmap

我试图下载多张图片&在列表视图中显示它。图像正在下载。但是当我向下滚动时,如果我回来,会下载不同的图像。例如:假设第1行有图像1,第2行:图像2等等。当我向下或向上滚动时,不同的图像就会出现在那个地方。如何避免这个问题。我从我的适配器调用了下载方法,如下所示:

适配器类

 public class ImageAdapter extends BaseAdapter{
    private final Context mContext;
   final String[] mImages;
    private LayoutInflater mInflater;
  public ImageAdapter(Context context, String[] images){
      mContext=context;
      mImages=images;
      mInflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mImages.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return mImages[position];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    private static class ViewHolder {
        ImageView imageView;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder=new ViewHolder();
            convertView=mInflater.inflate(R.layout.image_layout, parent, false);
            viewHolder.imageView=(ImageView) convertView.findViewById(R.id.image_View);
            convertView.setTag(viewHolder);
        }
        else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        String imageUrl=mImages[position];
        ImageDownloader downloader=new ImageDownloader();
        downloader.download(imageUrl,viewHolder.imageView);
        return convertView;

    }

}

然后在ImageDownloader类中,我调用了:

public void download(String url, ImageView imageView) {
         downloadBitmap(url, imageView);

     }

downloadBitmap具有assync任务方法:

private void downloadBitmap(final String url,final ImageView imageView) {

        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... params) {
                return downloadUrlToStream(url, imageView);
            }

            protected void onPostExecute(Bitmap bitmap) {
                imageView.setImageBitmap(bitmap);
            };
        }.execute();

    }

为什么我会得到不同的图像。任何人都可以建议我,我可能出错了吗?

1 个答案:

答案 0 :(得分:1)

    public class GalleryAdapter extends BaseAdapter {

    AlertDialog alertDialog = null;
    List<GalleryDTO> mDatalist;
    Activity context;
    private LruCache<String, Bitmap> mMemoryCache;
    LocationHandler mLocationHandler = null;
    public GalleryAdapter(Activity context, List<GalleryDTO> mDatalist) {
        this.context = context;
        this.mDatalist = mDatalist;
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
        final int cacheSize = maxMemory / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                return (int) (getSizeInBytes(bitmap) / 1024);
            }
        };

    }

    public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }

    public Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }

    public long getSizeInBytes(Bitmap bitmap) {
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            return bitmap.getByteCount();
        } else {
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    }

    @Override
    public int getCount() {

        return mDatalist.size();
    }

    @Override
    public Object getItem(int position) {

        return mDatalist.get(position);
    }

    @Override
    public long getItemId(int position) {

        return mDatalist.get(position).photoId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = context.getLayoutInflater().inflate(
                    R.layout.list_item_gallery, parent, false);
            holder = new ViewHolder();
            holder.mPhoto = (ImageView) convertView
                    .findViewById(R.id.img_photo);
            holder.mProgress = (ProgressBar) convertView
                    .findViewById(R.id.pg_list_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        GalleryDTO data = (GalleryDTO) getItem(position);
        loadBitmap(data.photoName, holder);

        return convertView;
    }



    private static class ViewHolder {
        protected ImageView mPhoto;
        protected ProgressBar mProgress;
    }

    public void loadBitmap(String respath, ViewHolder holder) {
        final String imageKey = respath;
        final Bitmap bitmap = getBitmapFromMemCache(imageKey);
        if (bitmap != null) {
            holder.mPhoto.setImageBitmap(bitmap);
            holder.mProgress.setVisibility(View.GONE);
        } else {
            if (cancelPotentialWork(respath, holder.mPhoto, holder.mProgress)) {
                final BitmapWorkerTask task = new BitmapWorkerTask(
                        holder.mPhoto, holder.mProgress);
                final AsyncDrawable asyncDrawable = new AsyncDrawable(
                        context.getResources(), holder.mPlaceHolderBitmap, task);
                holder.mPhoto.setImageDrawable(asyncDrawable);
                task.execute(respath);
            }
        }

    }

    class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
        private final WeakReference<ImageView> imageViewReference;
        private final WeakReference<ProgressBar> progressbarReference;
        private String data = "";

        public BitmapWorkerTask(ImageView imageView, ProgressBar progressBar) {
            imageViewReference = new WeakReference<ImageView>(imageView);
            progressbarReference = new WeakReference<ProgressBar>(progressBar);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            data = params[0];
            final Bitmap bitmap = downloadUrlToStream(url);
            addBitmapToMemoryCache(String.valueOf(params[0]), bitmap);
            return bitmap;

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (isCancelled()) {
                bitmap = null;
            }

            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                final ProgressBar mProgressBar = progressbarReference.get();
                final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(
                        imageView, mProgressBar);
                if (this == bitmapWorkerTask && imageView != null) {
                    imageView.setImageBitmap(bitmap);
                    if (mProgressBar != null)
                        mProgressBar.setVisibility(View.GONE);
                }
            }
        }
    }

    static class AsyncDrawable extends BitmapDrawable {
        private final WeakReference<BitmapWorkerTask> bitmapWorkerTaskReference;

        public AsyncDrawable(Resources res, Bitmap bitmap,
                BitmapWorkerTask bitmapWorkerTask) {
            super(res, bitmap);
            bitmapWorkerTaskReference = new WeakReference<BitmapWorkerTask>(
                    bitmapWorkerTask);
        }

        public BitmapWorkerTask getBitmapWorkerTask() {
            return bitmapWorkerTaskReference.get();
        }
    }

    public static boolean cancelPotentialWork(String data, ImageView imageView,
            ProgressBar progressBar) {
        final BitmapWorkerTask bitmapWorkerTask = getBitmapWorkerTask(
                imageView, progressBar);

        if (bitmapWorkerTask != null) {
            final String bitmapData = bitmapWorkerTask.data;
            if (bitmapData != data) {
                bitmapWorkerTask.cancel(true);
            } else {
                return false;
            }
        }
        return true;
    }

    private static BitmapWorkerTask getBitmapWorkerTask(ImageView imageView,
            ProgressBar progressBar) {
        if (imageView != null) {
            final Drawable drawable = imageView.getDrawable();
            if (drawable instanceof AsyncDrawable) {
                final AsyncDrawable asyncDrawable = (AsyncDrawable) drawable;
                return asyncDrawable.getBitmapWorkerTask();
            }
        }
        return null;
    }

}