Search code examples
androiduniversal-image-loaderandroid-pageradapter

Android PagerAdapter :: Get Current Item into bitmap


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.


Solution

  • 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();