Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an image capturing Fragment and I try to show as a ViewPager Gallery in the same fragment all the pictures taken.

The flow is: user clicks on a Take Image button, an intent opens the camera, he takes a photo and onActivityResult calls my preview images method which should make the ViewPager visible with all the taken photos.

I keep the photos in a global ArrayList<bitmap> named bitmaps.


global:
Java
ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
ImagePagerAdapter adapter = new ImagePagerAdapter();

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
.....
viewPager = (ViewPager) view.findViewById(R.id.view_pager);}

My previewCapturedImage method:
Java
private void previewCapturedImage() {
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 8;

        final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                options);

        bitmaps.add(bitmap);
        viewPager.setVisibility(View.VISIBLE);
        viewPager.setAdapter(adapter);

    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

My PagerAdapter:
Java
 private class ImagePagerAdapter extends PagerAdapter {

    @Override
    public int getCount() {
        return bitmaps.size();
    }
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((ImageView) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        int padding = 15;
        imageView.setPadding(padding, padding, padding, padding);
        imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        imageView.setImageBitmap(bitmaps.get(position));
        ((ViewPager) container).addView(imageView, 0);
        return imageView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((ImageView) object);
    }
}

my xml:
HTML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:removed="#ffffff"
android:orientation="vertical">
....

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/titlu2"
    android:layout_marginBottom="60dp"
    android:removed="#ffffff"
    android:orientation="vertical">


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/butoane_foto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:orientation="vertical">
        .....

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <!-- To display picture taken -->

            <android.support.v4.view.ViewPager
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/view_pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
                <!--android:visibility="gone"-->


        </LinearLayout>

         .....
    </LinearLayout>
</ScrollView>

  ...........
</RelativeLayout>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900