22

In my project I want to open a gallery on a button click and should be able to pick image or video to get path of them.

Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

From above code i am able to open gallery but in this case i am only able to choose image. So, please help me in choosing video also. Thanks in advance.

stack
  • 951
  • 3
  • 9
  • 23

9 Answers9

31

On Android 6.0 and above using "video/* image/" or "image/ video/*" type doesn't work, it only recognizes the first filter you specify. I solved the problem using this code:

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("*/*");
photoPickerIntent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {"image/*", "video/*"});
startActivityForResult(photoPickerIntent, Constants.SELECT_PHOTO);

Although this will ask the user which app they want to use to select the image/video.

YYamil
  • 1,100
  • 11
  • 11
22

You can use the next snippet:

Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);

But I think that it only work on ICS or bigger

Parker
  • 8,539
  • 10
  • 69
  • 98
Jc Miñarro
  • 1,391
  • 10
  • 18
11

You need use the following as picking Intent

Intent photoLibraryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoLibraryIntent.setType("image/* video/*");
KILA
  • 205
  • 3
  • 5
10

Below code solved my problem

  final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                        galleryIntent.setType("*/*");
                        startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
stack
  • 951
  • 3
  • 9
  • 23
9

2022 Android 9

I've tried everything available online, and accidently mixed 2 solutions that turned out to work.

This only gives photo library and google photos as options, and you can select photos AND videos.

    libraryIntent.setType("video/*, image/*");
    String[] mimetypes = {"image/*", "video/*"};
    libraryIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
Gabriel Petersson
  • 8,434
  • 4
  • 32
  • 41
8

Not enough rep to comment, but @YYamil's response works well.

Intent mediaChooser = new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, image/*");
startActivityForResult(mediaChooser, RESULT_LOAD_IMAGE);

If you're using the new registerForResultActivity, create a copy of ActivityResultContracts.GetMultipleContents() and put in createIntent:

    @CallSuper
    override fun createIntent(context: Context, input: String): Intent {
        return Intent(Intent.ACTION_GET_CONTENT)
            .addCategory(Intent.CATEGORY_OPENABLE)
            .setType(input)
            .putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
            .putExtra(Intent.EXTRA_MIME_TYPES, arrayOf("image/*", "video/*")) // this does the trick
    }
Rodrigo Alfonso
  • 129
  • 1
  • 6
4

This is the best i known...... Try this for once....

 final CharSequence[] options = {"Images", "Videos", "Cancel"};
            AlertDialog.Builder builder = new AlertDialog.Builder(OpenGallery.this);
            builder.setTitle("Select From...");
            builder.setItems(options, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int item) {
                    if (options[item].equals("Images")) {
                        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                        startActivityForResult(intent, 1);
                    } else if (options[item].equals("Videos")) {
                        Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                        startActivityForResult(intent, 1);
                    } else if (options[item].equals("Cancel")) {
                        dialog.dismiss();
                    }
                    dialog.dismiss();
                }
            });
            builder.show();
Ramaraju
  • 604
  • 1
  • 6
  • 17
3

Change your Intent to this:

Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

When trying to get videos you need to state to mediaStore that video is in order and not images as you wrote.

MaTriXy
  • 1,659
  • 1
  • 20
  • 27
0

Well android has put a lot of restriction on accessing the external content. I ended up using 3rd party library. This one is good.: https://github.com/AnilFurkanOkcun/UWMediaPicker-Android

implementation 'com.github.AnilFurkanOkcun:UWMediaPicker-Android:1.3.0'


UwMediaPicker
.with(this)                     // Activity or Fragment
    .setGalleryMode(UwMediaPicker.GalleryMode.ImageGallery) // GalleryMode: ImageGallery/VideoGallery/ImageAndVideoGallery, default is ImageGallery
.setGridColumnCount(4)                                  // Grid column count, default is 3
    .setMaxSelectableMediaCount(10)                         // Maximum selectable media count, default is null which means infinite
    .setLightStatusBar(true)                                // Is llight status bar enable, default is true
.enableImageCompression(true)               // Is image compression enable, default is false
.setCompressionMaxWidth(1280F)              // Compressed image's max width px, default is 1280
.setCompressionMaxHeight(720F)              // Compressed image's max height px, default is 720
.setCompressFormat(Bitmap.CompressFormat.JPEG)      // Compressed image's format, default is JPEG
.setCompressionQuality(85)              // Image compression quality, default is 85
.setCompressedFileDestinationPath(destinationPath)  // Compressed image file's destination path, default is "${application.getExternalFilesDir(null).path}/Pictures"
.launch{selectedMediaList-> } // (::onMediaSelected)    // Will be called when media is selected
M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69