1

I would like some help on integrating two pieces of functionality from my code as I am stuck and require some help with it.

Basically what I want to develop is a slider where the user gets to upload images from their gallery onto the slider and flick through the slider to see their uploaded pictures. I would like a maximum of 20 images and lets say there's 5 images uploaded, it will be nice to start from first image, slide all the way to the fifth image and then it goes back to image number one after flicking fifth image.

At the moment I have a piece of code that is ale to upload an image from the gallery and another piece of code that uses PageViewer to create the image slider (however these images are images already stored within drawable for now).

I would be the happiest person if this can be implemented because it is the only part of the app I am stuck on.

Below are the relevant code:

PhotosActivity - code that uploads image from gallery:

    private static int loadImageResults = 100;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photos);

        uploadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openGallery();
            }
        });

...

    public void openGallery() {
        Intent intentImageContent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intentImageContent, loadImageResults);
    }

PhotoActivity - code that calls on the PhotosPageViewerAdapter class for the image slider:

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_photos);

...

PhotosViewPagerAdapter photosViewPagerAdapter = new PhotosViewPagerAdapter(PhotosActivity.this);
        photosSlider.setAdapter(photosViewPagerAdapter);

PhotosPageViewerAdapter class:

public class PhotosViewPagerAdapter extends PagerAdapter {

    private Context context;
    private LayoutInflater layoutInflater;
    private Integer [] images = {R.drawable.question, R.drawable.love_heart, R.drawable.love_heart_dark};

    public PhotosViewPagerAdapter(Context context){
        this.context = context;
    }


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

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

    @Override
    public Object instantiateItem(ViewGroup container, int position){
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.activity_photos, null);
        ImageView imageView = view.findViewById(R.id.image_content);
        imageView.setImageResource(images[position]);

        ViewPager vp  = (ViewPager) container;
        vp.addView(view, 0);
        return view;

    }

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

Finally the activity_photos.xml where it contains the image view from the upload image and the page viewer for the slider, preferable the uploaded image will go into the page viewer.

<ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:id="@+id/image_content"/>

        <android.support.v4.view.ViewPager
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/photos_slider">
        </android.support.v4.view.ViewPager>

Thank you very much

Pristin
  • 175
  • 1
  • 2
  • 13

1 Answers1

1

I will guide you only where you need it.

1. Image multi pick

Android does not support image multi select by default, so you need to use a separate library like this. https://github.com/luminousman/MultipleImagePick

2. send selected image

EXAMPLE

private void pickImage(){
    Intent intentImageContent = new Intent(Intent.ACTION_PICK, 
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intentImageContent, loadImageResults);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == loadImageResults) {
        if (resultCode == RESULT_OK && data != null) {
            Intent intent = new Intent(this, Main3Activity.class);
            intent.putExtra("pickImage", data.getData());
            startActivity(intent);
        }
    }
}

3. receive PhotoActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);

    if (getIntent() != null) {
        Uri imageUri = getIntent().getParcelableExtra("pickImage");
    }
 }

4. load image I recommend use imageloader such as Glide

private void loadImage(Uri uri){
    Glide.with(context).load(uri).into(yourImageView)
}
pistolcaffe
  • 849
  • 1
  • 8
  • 15
  • @Pristin I hope you have a good result. If you have any additional questions, please leave a comment. I will help you. – pistolcaffe Jun 22 '18 at 05:15
  • Ok so GLide I can follow a youtube tutorial on. have downloaded the library but I am a little confused with the lumionous. I am unsure if I should follow the code under Usage in this link or the code for point 2: https://github.com/luminousman/MultipleImagePick. Also in tha link is a stackoverflow question where a person managed to put maximum number of images selected. Does that go in the PhotosActivity or does it require an additional class to do this? – Pristin Jun 22 '18 at 05:31
  • point2: code is just an example that sends the selected image uri data to another activity on a single pick basis. If you do not need it, you may not use it. In your situation, if you need multi select, use a library like MultipleImagePick. Can you inform me a link to the stackoverflow question you have seen? – pistolcaffe Jun 22 '18 at 06:01
  • Sure it's here: https://stackoverflow.com/questions/4722928/intent-for-getting-multiple-images/15029515#15029515 – Pristin Jun 22 '18 at 06:05
  • I can follow the example code in the multipleImagepick for multiple images and give it a go. I want to post the whole code to help others once I'm done as well but yeah will require some help with implementation possibly if I get stuck so I will keep you updated. I am a novice programmer so learning a lot :) – Pristin Jun 22 '18 at 06:07
  • I think https://github.com/esafirm/android-image-picker this library is also good. Do you have to set the maximum to 5 in your case? – pistolcaffe Jun 22 '18 at 06:22
  • I'm using 5 for test purposes. Once working I'll increase it to like 50. Thanks I will give the imagepicker a go first as that does look good – Pristin Jun 22 '18 at 06:45
  • Hi pistolcaffe, ler me know when you're available and we can move the discussion to chat. I am on the last point where trying to implement with glide and just require some help displaying it – Pristin Jun 23 '18 at 07:13
  • Oh my god my reputation points is not enough for chat. Let me just try something and if it fails then can I send you my email with the issue displayed inside? – Pristin Jun 23 '18 at 08:52
  • sure. my email is pistolcaffe@naver.com – pistolcaffe Jun 23 '18 at 09:05
  • Thanks, ok what I tried did not work so I email you the problem and attach the relevant pages with the code so you can see what I've done – Pristin Jun 23 '18 at 10:05
  • email sent, look for the subject: Glide Photos Issue . Thank you very much and send me a reply if you have any questions – Pristin Jun 23 '18 at 10:30
  • Oh I commented out //intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); just to get single image upload working first. This just needs to be uncommented for multi to work – Pristin Jun 23 '18 at 10:34