-2

I am getting the user chosen images from the gallery through onActivityResult() as follow:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == OPEN_MEDIA_PICKER) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK && data != null) {

            ArrayList<String> selectionResult = data.getStringArrayListExtra("result");


            System.out.println(selectionResult);
        }
    }
}

When I try to print the selectionResult to the console, it prints the directory of every image as string to the console.

How can I store the actual images in an array and send them through the intent to the next activity?

Pang
  • 9,564
  • 146
  • 81
  • 122
Source
  • 99
  • 1
  • 1
  • 7
  • 1
    Possible duplicate of [Passing image from one activity another activity](https://stackoverflow.com/questions/11519691/passing-image-from-one-activity-another-activity) – Tim Biegeleisen Oct 31 '17 at 01:59
  • @TimBiegeleisen I want to pass an array of images, not just one – Source Oct 31 '17 at 02:08
  • The first answer will still work for you. Just make however many entries in your intent map you need to cover all images. – Tim Biegeleisen Oct 31 '17 at 02:13
  • 3
    Possible duplicate of [Android - how to pass a List of Bitmap to another Activity as parameter](https://stackoverflow.com/questions/39778238/android-how-to-pass-a-list-of-bitmap-to-another-activity-as-parameter) – Enzokie Oct 31 '17 at 02:27

3 Answers3

2

Bro, It is not good approach to send bitmap data of image from one activity to another because bitmap object be mostly large. So outOfMemory Issue may be generated and your application will be crash due to this outOfMemory. So You should use image director instead of bitmap data. I means you send image path(image directory) from one activity to other activity. You send ArrayList object of image path through intent from one activity to another activity. because it be serializable and String format data is not large.

  • I see, thank you, I have applied your way and stored the images directory in an ArrayList, but when I want to send them to the database (Firebase) I should convert them to array of Bitmap, am I right? – Source Oct 31 '17 at 19:20
2

you just create Bitmap array Then generate Bitmap Using URI and put in Array then pass Array to Adapter.

ArrayList<Bitmap> bitmaps =new ArrayList<>();

then

Uri uri = data.getData();

Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);

bitmaps .add(bitmap );

then notify adapter.

nirav karad
  • 111
  • 1
  • 4
1

you can add a file path in arraylist like :

  arraylist.add(file.getAbsolutePath());
  adapter.notifyDataSetChanged();

then send arraylist in new Activity and get image form file in new activity like :

    FileInputStream streamIn = null;
    try {
        streamIn = new FileInputStream(new File(arraylist.get(position)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(streamIn); //This gets the image
    custom_imageView.setImageBitmap(bitmap);
nirav karad
  • 111
  • 1
  • 4