Hayk Mkrtchyan
Hayk Mkrtchyan

Reputation: 3245

How do I set images from gallery to slider?

I have one fragment, there is an imageView and 2 buttons (previous and next). So in that imageView I'm adding the first picture from gallery. Then clicking next I want to see the next image from gallery. The path of images I already got.

Let me show you the code.

public class PhotoFragment extends BaseFragment{
View mainView;
private ImageView photoView;
private Button prev, next;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mainView = inflater.inflate(R.layout.photo_fragment, container, false);

    photoView = (ImageView) mainView.findViewById(R.id.photoView);
    prev = (Button) mainView.findViewById(R.id.prevPhoto);
    next = (Button) mainView.findViewById(R.id.nextPhoto);

    getAllShownImagesPath(getActivity());
    return mainView;
}

ArrayList<String> getAllShownImagesPath(Activity activity) {
    Uri uri;
    Cursor cursor;
    int column_index_data;
    final ArrayList<String> listOfAllImages = new ArrayList<>();
    String absolutePathOfImage;
    uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

    String[] projection = {MediaStore.MediaColumns.DATA,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME};

    cursor = activity.getContentResolver().query(uri, projection, null,
            null, null);

    column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    while (cursor.moveToNext()) {
        absolutePathOfImage = cursor.getString(column_index_data);
        listOfAllImages.add(absolutePathOfImage);
    }

    Bitmap myBitmap = BitmapFactory.decodeFile(listOfAllImages.get(0));
    photoView.setImageBitmap(myBitmap);
    photoView.setRotation(90);

    Bitmap myBitmap1 = BitmapFactory.decodeFile(listOfAllImages.get(1));
    Bitmap myBitmap2 = BitmapFactory.decodeFile(listOfAllImages.get(2));
    Bitmap myBitmap3 = BitmapFactory.decodeFile(listOfAllImages.get(3));
    Bitmap myBitmap4 = BitmapFactory.decodeFile(listOfAllImages.get(4));
    Bitmap myBitmap5 = BitmapFactory.decodeFile(listOfAllImages.get(5));
    Bitmap myBitmap6 = BitmapFactory.decodeFile(listOfAllImages.get(6));

    final ArrayList<Bitmap> bitmap = new ArrayList<>();
    bitmap.add(myBitmap);
    bitmap.add(myBitmap1);
    bitmap.add(myBitmap2);
    bitmap.add(myBitmap3);
    bitmap.add(myBitmap4);
    bitmap.add(myBitmap5);
    bitmap.add(myBitmap6);


    prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });

    cursor.close();
    return listOfAllImages;
}

You see here I'm getting the first image and setting it in imageView.

Bitmap myBitmap = BitmapFactory.decodeFile(listOfAllImages.get(0));
    photoView.setImageBitmap(myBitmap);

But I can't do the sliding part. How can I do this?

Upvotes: 0

Views: 86

Answers (1)

Esperanz0
Esperanz0

Reputation: 1586

int imagePosition = 0;

next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          imagePosition++;
      photoView.setImageBitmap(BitmapFactory.decodeFile(listOfAllImages.get(imagePosition)););
          photoView.setRotation(90);
        }
    });

prev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          imagePosition--;
      photoView.setImageBitmap(BitmapFactory.decodeFile(listOfAllImages.get(imagePosition)););
          photoView.setRotation(90);
        }
    });

Upvotes: 1

Related Questions