Search code examples
androidandroid-intentgallery

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

Solution

  • Try this:

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