I've created a sample here to reproduce this issue. I'm running this project on an emulator with API level 29 and selecting a video from google photos provider. But I cannot figure out how to get the mimeType of the selected file. I've used ContentResolver's getType method which returns null and also ContentResolver query method which somehow throws an IllegalArgumentException to me. I've attached a screenshot for the same.
Note: Everything works fine if I select the same file by navigating through the directories, but when I use the google photos provider, it fails.
This is my code::
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK && requestCode == IMAGE_VIDEO_FETCH) {
Uri selectedFileURI = intent.getData();
String type = this.getContentResolver().getType(selectedFileURI);
if (type == null || TextUtils.isEmpty(type)) {
// This shouldn't happen right?
// Since https://developer.android.com/training/secure-file-sharing/retrieve-info
// mentions a FileProvider determines the file's MIME type from its filename extension. I've to add this block
Cursor cursor = null;
try {
cursor = getContentResolver().query(selectedFileURI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int colIndex = cursor.getColumnIndex(MediaStore.MediaColumns._ID);
type = cursor.getString(colIndex);
}
} catch (IllegalArgumentException e) {
Log.d("Check Type :: ", type + "");
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
} else {
Log.d("Type:: ", type + "");
}
}
}
And this is how I start an intent::
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/*");
startActivityForResult(i, IMAGE_VIDEO_FETCH);
Can anyone help me out with the correct way to figure out the mimetype of the selected file? PS: I've tried ContentResolver.getType, MimeTypeMap and ContentResolver.query. None of them works.