Define in onActivityResult if image from gallery or video is selected - Android

I use the following code to select an image or video from the gallery:

    imgGallery.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent pickPhoto = new Intent(Intent.ACTION_GET_CONTENT);
                    pickPhoto.setType("*/*");
                    String[] mimetypes = {"image/*", "video/*"};
                    pickPhoto.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
                    startActivityForResult(pickPhoto,
                            SELECT_PICTURE_OR_VIDEO);
                }
            });

Please note that I use the same button to select an image or video. So, when onActivityResult will be called, is there any way from which I can know that the image was selected or the video was selected from the gallery?

+4
source share
3 answers

You can check the code below.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && data.getData() != null) {
        String path = data.getData().getPath();
        if (path.contains("/video/")) {
            Log.d(this.getClass().getName(), "Video");
        } else if (path.contains("/images/")) {
            Log.d(this.getClass().getName(), "Image");
        }
    }
}

, , , , , , , - /external/images/media/2928, , URI Android.

+1

, , , , images/video Android .

Content Resolver mime:

ContentResolver cr = mContext.getContentResolver();
String mime = cr.getType(uri);

:

public void onActivityResult(int requestCode, int resultCode, Intent mediaReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, mediaReturnedIntent);

    Uri selectedMedia = mediaReturnedIntent.getData();
    ContentResolver cr = mContext.getContentResolver();
    String mime = cr.getType(selectedMedia);
    if(mime.toLowerCase().contains("video")) {
        // Do something with the video
    } else if(mime.toLowerCase().contains("image")) {
        // Do something with the image
    }
}
+1

, . startActivityForResult , ,

public void onClick(View v) {
            Intent pickPhoto = new Intent(Intent.ACTION_GET_CONTENT);
            pickPhoto.setType("*/*");
            String[] mimetypes = {"image/*", "video/*"};
            pickPhoto.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
            startActivityForResult(pickPhoto, 1);

it's the same as yours

and in OnActivityResult I did not add any conditions. Easiest is

    public void onActivityResult(int i,int j, Intent intent) {
        super.onActivityResult(i, j, intent);
}

and when I debug the value in intention, I got the following: when the image is selected

Intent { dat=content://com.android.providers.media.documents/document/image:211660 flg=0x1 }

and when choosing a video

Intent { dat=content://com.android.providers.media.documents/document/video:169729 flg=0x1 }

Here you can see that it is easy to identify what is selected. No work required for this.

0
source

Source: https://habr.com/ru/post/1621929/


All Articles