Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android. How to determine the type of media file in onActivityResult()?

I get media file from Gallery.

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*,video/*,audio/*");
            startActivityForResult(Intent.createChooser(intent, "Select Media"), CameraUtils.REQUEST_GALLERY);

How to determine the type of media file in onActivityResult()?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) {
        switch (requestCode) {
            case CameraUtils.REQUEST_GALLERY:
                Uri uri = data.getData();
                //what type of media file?
                break;
        }
    }
}
like image 872
user1528799 Avatar asked Nov 29 '22 23:11

user1528799


1 Answers

Try this:

ContentResolver cr = this.getContentResolver();
String mime = cr.getType(uri);
like image 116
Prerak Sola Avatar answered Feb 15 '23 23:02

Prerak Sola