Restrict upload from Android Photo's instead of gallery

1k Views Asked by At

As you might have noticed on newer Android versions, Android now has 2 image galleries: gallery and photos. In the future, Google will deprecate and remove the gallery and only have the photos library.

I want to give users the ability to select multiple images for upload using the Android API level 18+ ".putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);". The problem is, if the user chooses to upload from the gallery, the functionality doesn't work (only one picture will be selectable).

How can I make sure the app automatically tries to upload through the Android Photo's app?

This is my code so far:

galerijButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
        if(Build.VERSION.SDK_INT >= 18 ){
            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);
                    // more code logic
                }
            }
        }
    break;
1

There are 1 best solutions below

3
On

You can use intent with action which have package name for call photo app. Example com.google.exactnameofphotoapp. it then causes the application that you want.

And use

 if(Build.VERSION.SDK_INT >= 18 ){
...
}

for new api features.