1

I am using UniversalImageLoader for my application. My ImageAdapter extends PagerAdapter. I need currentImage as Bitmap. By callback methood public void onLoadingComplete(Bitmap loadedImage) i am getting bitmap. But that is not that bitmap image currently showing. Either it is the next image or previous image (depending upon swaping). Here is the full code:

    private class ImagePagerAdapter extends PagerAdapter {
    private String[] images;
    private LayoutInflater inflater;

    ImagePagerAdapter(String[] images) {
        this.images = images;
        inflater = getLayoutInflater();
    }

    @Override
    public void destroyItem(View container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {
        return images.length;
    }

    @Override
    public Object instantiateItem(View view, int position) {
        if (view != null) {
            final View imageLayout = inflater.inflate(
                    R.layout.item_pager_image, null);
            final ImageView imageView = (ImageView) imageLayout
                    .findViewById(R.id.image_item_pager);

            imageLoader.displayImage(images[position], imageView, options,
                    new ImageLoadingListener() {

                        @Override
                        public void onLoadingStarted() {
                        }

                        @Override
                        public void onLoadingFailed(FailReason failReason) {
                            //On Loading Failed Logic                               
                        }

                        @Override
                        public void onLoadingComplete(Bitmap loadedImage) {
                                                        bitmap = loadedImage;  
                            imgViewSetAsWp.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onLoadingCancelled() {
                            // Do nothing
                        }
                    });


            ((ViewPager) view).addView(imageLayout, 0);

            return imageLayout;
        } else
            return view;
    }

    @Override
    //Other override methood of ImagePager

}

Is there any way to get the proper bitmap.

ratulalahy
  • 460
  • 2
  • 8
  • 16

2 Answers2

1

How to get current view - https://stackoverflow.com/a/8638772/913762

and then:

View currentView = ...
ImageView currentImageView = (ImageView) currentView.findViewById(...);
BitmapDrawable bd = (BitmapDrawable) currentImageView.getDrawable();
Bitmap bmp = bd.getBitmap();
Community
  • 1
  • 1
nostra13
  • 12,377
  • 3
  • 33
  • 43
0

The problem comes when you inflate view: it's the same single instance that is used throughout the list, so when your asynchronous loading is complete, the image is changed when the viewpager is trying to paint a different item using the same imageView. Here is how I fix mine.

1)Create a class named ViewHolder

class ViewHolder {
    public ImageView image;
}

2) As soon as you finish inflating, create new ViewHolder object and bind it.

ViewHolder holder = new ViewHolder();
holder.image = (ImageView) imageLayout.findViewById(R.id.image_item_pager);

After that, you can use holder.image as normal ImageView.

Hope this help.

Hein
  • 2,675
  • 22
  • 32
  • Thanks for replay. My ImagePager is working fine. On the imageView exact image is loaded. Problem is getting the exact bitmap. `@Override public void onLoadingComplete(Bitmap loadedImage) { bitmap = loadedImage; }`This function load next or previous image after setting correct image. I need current bimap image. – ratulalahy Dec 29 '12 at 07:44
  • How about changing the method. `imageLoader.displayImage(images[position], imageView)` – Hein Dec 29 '12 at 07:51
  • Latest version of `UniversalImageLoader` library has `displayImage` method which require only `image link` and `imageview` as parameters. I've been using it for a while. – Hein Dec 30 '12 at 04:14
  • i was searching for currently displaying image in bitmap. @nostra 's solution worked. Thanks :) – ratulalahy Jan 03 '13 at 18:07