2

I'm trying to populate my Gridview with thumbnail images but I'm having issues with decoding the images within the asynctask. When I start the app, images are loaded 1 by 1 and Images are not displayed correctly ( Once I scroll it shows top image and loads the original later). Here is my code :

public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> mList;
    private int mheight;
    private int mwidth;
    private InputStream is;

    public ImageAdapter(Context context, List<String> list, int height, int width) {
        mContext = context;
        mList = list;
        mheight = height;
        mwidth = width;
    }


    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position).toString();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }


        InputStream is;
        try {
            is = mContext.getAssets().open(mList.get(position));
            Loadimage task = new Loadimage(imageView , mheight , mwidth);
            task.execute(is);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return imageView ;

    }

    public class Loadimage extends AsyncTask<InputStream, Void, Bitmap>{
        private final WeakReference<ImageView> imageViewReference;


        private InputStream is = null;
        private int width;


        public Loadimage(ImageView imageView, int mheight, int mwidth) {
             imageViewReference = new WeakReference<ImageView>(imageView);
             this.width=mwidth;

            // TODO Auto-generated constructor stub
        }

        @Override
        protected Bitmap doInBackground(InputStream... params) {
            is = params[0];

            if (is !=null) {

                Bitmap bitmap  = BitmapFactory.decodeStream(is);
                Bitmap nBitmap =Bitmap.createScaledBitmap(bitmap,width/3 , width/3, false);
                return nBitmap;     
            }
            return null; 
          }
         @Override
         protected void onPostExecute(Bitmap bitmap) {
            if (imageViewReference != null && bitmap != null) {
                final ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                }


        }
    }
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
Joseph27
  • 129
  • 3
  • 16
  • why don't you use Universal Image Loader or Lazy list?. Also use a viewholder for smooth scrolling and performance. – Raghunandan Mar 24 '13 at 15:35

1 Answers1

1

I highly recommend Universal Image Loader. It has just what you need: a gridview that can be loaded with thumbnails. It also has memory and disk cache features, along with logging and features for thread pool sizes. Give it a try; it works perfectly for me and it should fit your needs.

Kgrover
  • 2,106
  • 2
  • 36
  • 54
  • i tried to use universal image loader but i got an issue here my new post http://stackoverflow.com/questions/15618989/gridview-with-universal-image-loader-images-are-loading-slowly – Joseph27 Mar 25 '13 at 15:44