1

I need to Load Images from a folder in SD Card into a ViewPager. This is what i have managed so far.

public Drawable getImageFromSdCard(String imageName) {
    Drawable d = null;
    try {
        String path = Environment.getExternalStorageDirectory().toString()
                + "/YourSubDirectory/";
        Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
                + ".png");
        d = new BitmapDrawable(bitmap);
    } catch (IllegalArgumentException e) {
        // TODO: handle exception
    }
    return d;

}

How can i set this as a ViewPager ?

Mridul S Kumar
  • 326
  • 1
  • 4
  • 20
  • Refer this tutorial http://codetheory.in/android-image-slideshow-using-viewpager-pageradapter/ – Durgesh Patel Aug 20 '15 at 06:34
  • Hey @Mridul S Kumar....i have same Queries to set last captured images into ViewPager......my Question link is.............http://stackoverflow.com/questions/42066564/view-pager-can-not-display-last-captured-images – Sagar Aghara Feb 07 '17 at 06:04
  • Please tell me if you know....i can not fetch last captued Images in View pager...Please once refer my question..because its seem's there is No Error their...): – Sagar Aghara Feb 07 '17 at 06:05

1 Answers1

1

You need make adapter that extends PagerAdapter then set adapter to view pager

Try below code:

ViewPager mViewPager = (ViewPager) findViewById(R.id.view_pager);
mViewPager.setAdapter(new TouchImageAdapter());

TouchImageAdapter class:

class TouchImageAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            ImageView img = new ImageView(container.getContext());
            img.setImageDrawable(getImageFromSdCard(filename));
            container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            return img;
        }

        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }


    }

Here is your method used in adapter class

public Drawable getImageFromSdCard(String imageName) {
    Drawable d = null;
    try {
        String path = Environment.getExternalStorageDirectory().toString()
                + "/YourSubDirectory/";
        Bitmap bitmap = BitmapFactory.decodeFile(path + "/" + imageName
                + ".png");
        d = new BitmapDrawable(bitmap);
    } catch (IllegalArgumentException e) {
        // TODO: handle exception
    }
    return d;

}

Hope this helps!

Rajesh Jadav
  • 12,801
  • 5
  • 53
  • 78
  • Any idea how to add pinch zoom to the imageview ? – Mridul S Kumar Aug 20 '15 at 09:11
  • You have make custom class for zooming functionality to imagview . this http://stackoverflow.com/questions/7491519/how-does-touchimageview-works helps – Rajesh Jadav Aug 20 '15 at 09:13
  • Binary XML file line #1: Error inflating class com.example.touch.ExtendedViewPager app crashes – Mridul S Kumar Aug 20 '15 at 09:37
  • Hey Rajesh Bro....i have same Queries to set last captured images into ViewPager......my Question link is.............http://stackoverflow.com/questions/42066564/view-pager-can-not-display-last-captured-images – Sagar Aghara Feb 07 '17 at 05:45
  • Rajesh Bro. plz ke bhai jo tane Khabar hoi to...kem ke 2 days thaya solve karu chu...and bhul pan dekhati nathi.....); – Sagar Aghara Feb 07 '17 at 05:57