Not a duplicate to Choosing photo using new Google Photos app is broken
My app requests for file which can be images or videos and we only support some of the file extensions. And since MediaStore.MediaColumns.Data is deprecated, I've to go with the fileDescriptor approach to solve this. But I'm not able to figure out a way to get the extension of selected file.
The URI I get after selecting a video from the Google Photos app looks like this:
content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/643532680
When I try to query ContentResolver using _data column it gives an IllegalArgumentException to me, so I used below code to sort of copy the file to a temporary location.
String[] str = uri.toString().split("/");
String fileName = str[str.length - 1];
String filePath = context.getFilesDir().getAbsoluteFile() + File.separator + fileName;
File newFile = new File(filePath);
FileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r").getFileDescriptor();
FileInputStream fis = new FileInputStream(fileDescriptor);
copyFile(fis, newFile);
return filePath;
So, how should I get the correct mimeType of the selected file?
PS: I tried MimeTypeMap, ContentResolver.getType but none of them works.
EDIT: Adding more details: I'm using api level 29. Running the app on a Nexus 6(API 29) emulator. And simply calling ACTION_GET_CONTENT to allow user to select an image/video. Also included the URI I get after selecting the photo which doesn't contain mimetype(which as suggested by CommansWare is normal). When I call ContentResolver.getType on the above URI I get null.
EDIT 2:
Minimal code to reproduce: Started an intent like this:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/*");
startActivityForResult(i,IMAGE_VIDEO_FETCH);
Then in the onActivityResult, checked the mimetype.
Uri media = intent.getData();
this.getContentResolver().getType(media); /// Here it returns null.