11

I'm creating an image uploader that has the ability to upload more than 1 images at once.

galerijButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        photoPickerIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

        photoPickerIntent.setType("image/*");
        startActivityForResult(photoPickerIntent, SELECT_PHOTO);   
    }
});

...

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case SELECT_PHOTO:
        if(resultCode == RESULT_OK){  
            if(imageReturnedIntent.getData() != null){
                //If uploaded with Android Gallery (max 1 image)
                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
                    photos.add(yourSelectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } else {
                //If uploaded with the new Android Photos gallery
                ClipData clipData = imageReturnedIntent.getClipData();
                for(int i = 0; i < clipData.getItemCount(); i++){
                    clipData.getItemAt(i);
                    //What now?
                }
            }
        }
    break;
    ....

I would like to add all the selected images to my photos array which is an ArrayList<Bitmap> . Somehow I have to convert the ClipData.Item to Bitmap, but how?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Bart Burg
  • 4,786
  • 7
  • 52
  • 87
  • are you trying to copy images from clipboard? – SMR Feb 13 '14 at 10:30
  • No, I'm using the Android Photos app to upload multiple images at once. The Android Photos app puts the selected images in (Intent) imageReturnedIntent.getClipData(); I know this, because the size of that Array is always the number of selected images. – Bart Burg Feb 13 '14 at 10:32

3 Answers3

22

try this

ClipData.Item item = clip.getItemAt(i);
Uri uri = item.getUri();

now you have the URIs of the images.

I guess you know what to do now. Cheers :)

danb
  • 10,239
  • 14
  • 60
  • 76
SMR
  • 6,628
  • 2
  • 35
  • 56
2

Achieved with a Kotlin one liner:

private fun ClipData.convertToList(): List<Uri> = 0.until(itemCount).map { getItemAt(it).uri }
Community
  • 1
  • 1
Haider Malik
  • 1,581
  • 1
  • 20
  • 23
0

You encode image to string and when you want to use it decode it back to image.

For image list make a string array and save those images in that.

public static String encodeTobase64(Bitmap image) {
    Bitmap immage = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
    Log.d("Image Log:", imageEncoded);
    return imageEncoded;
}

public static Bitmap decodeBase64(String input) {
    try {
        byte[] encodeByte = Base64.decode(input, Base64.DEFAULT);
        InputStream is = new ByteArrayInputStream(encodeByte);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Config.ALPHA_8;
        options.inSampleSize = 2;
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, options);
        return bitmap;
    } catch (Exception e) {
        e.getMessage();
        return null;
    }
}
Vikas Rathod
  • 374
  • 2
  • 14