Identify in onActivityResult if image was selected from gallery or video was selected - Android
Asked Answered
P

3

6

I am using following code to select a image or video from 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 I am using same button for image or video selection. So when onActivityResult will be called, is there any way from which I can know that a image was selected or video was selected from the gallery?

Pyrone answered 28/12, 2015 at 6:52 Comment(2)
can you check for mime types in onActivityResult??Vanatta
@VivekMishra : I was looking for that but I didnt find it anywhere.Pyrone
S
4

You can check with below code.

@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");
        }
    }
}

This will work definitely because the path which we will get is something like I didn't find anything other than this, this will work definitely because the path it returns something like this /external/images/media/2928 where we will not get any repetitive data. Because the URI here only contains the ID of the image in the database of android data store.

Salonika answered 28/12, 2015 at 7:6 Comment(9)
Yes I had this in my mind, but if somehow image name has some string like 'video' in it then it will be considered as video isnt it?Pyrone
Yes Now I think it will not have any issue. But still this appears as a work around. I mean no full proof solution we have? something like get the mime type from the data to know if its image or videoPyrone
what will you get if you do getExtra in onActivityResult?? I think you should get mimetype in thatVanatta
@VivekMishra its coming null.Salonika
Accepted the answer until we get some more accurate answer to this. Thanks Vivek and Nigam.Pyrone
Will not work when data.getData().getPath() returns /document/image:98 or /document/video:98. This is on Android 7.1Embrasure
@Embrasure need to check. Will update it accordingly.Salonika
I guess that the best way to be sure would be to separate image and video request codes and create separate intents. Still looking for a solution when you use the same intent.Embrasure
@Nigam Patro Ok so according to this post, it can be done using the ContentResolver. This way you can be sure that you will get mime type on any Android version ContentResolver cr = getActivity().getContentResolver(); String mime = cr.getType(uri) Now I can see that the comment below is using the same method to get mime typeEmbrasure
B
8

Using the path as suggested by the other answers here isn't the best way of doing this because there is no guarantee that the path will always include the words images / video on all versions of Android on all devices forever.

Instead try using Content Resolver to get the mime type:

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

You can use it like this

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
    }
}
Bebe answered 30/7, 2017 at 9:58 Comment(0)
S
4

You can check with below code.

@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");
        }
    }
}

This will work definitely because the path which we will get is something like I didn't find anything other than this, this will work definitely because the path it returns something like this /external/images/media/2928 where we will not get any repetitive data. Because the URI here only contains the ID of the image in the database of android data store.

Salonika answered 28/12, 2015 at 7:6 Comment(9)
Yes I had this in my mind, but if somehow image name has some string like 'video' in it then it will be considered as video isnt it?Pyrone
Yes Now I think it will not have any issue. But still this appears as a work around. I mean no full proof solution we have? something like get the mime type from the data to know if its image or videoPyrone
what will you get if you do getExtra in onActivityResult?? I think you should get mimetype in thatVanatta
@VivekMishra its coming null.Salonika
Accepted the answer until we get some more accurate answer to this. Thanks Vivek and Nigam.Pyrone
Will not work when data.getData().getPath() returns /document/image:98 or /document/video:98. This is on Android 7.1Embrasure
@Embrasure need to check. Will update it accordingly.Salonika
I guess that the best way to be sure would be to separate image and video request codes and create separate intents. Still looking for a solution when you use the same intent.Embrasure
@Nigam Patro Ok so according to this post, it can be done using the ContentResolver. This way you can be sure that you will get mime type on any Android version ContentResolver cr = getActivity().getContentResolver(); String mime = cr.getType(uri) Now I can see that the comment below is using the same method to get mime typeEmbrasure
V
0

You can detect whether it is an image or video .I tried your code from startActivityForResult with mine and I was able to detect if image is selected or a video here is what I did

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);

this is same as yours

and in OnActivityResult I added no condition.Simply like this

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

and when I debug for value in intent I got this: when image is selected

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

and when video is selected

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

Here you can see it is easily identifiable that what is selected.No work around is needed for that

Vanatta answered 28/12, 2015 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.