0

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. 
Shivam Pokhriyal
  • 1,044
  • 11
  • 26
  • 1
    Use context.getContentResolver().query() on the content resolver for the obtained uri and look at MIME_TYPE in the obtained cursor. You can add DISPLAY_NAME to obtain the original file name as that number you get now is of little value. – blackapps Feb 11 '20 at 11:47
  • @blackapps I've added more details. Hope this will make the question clear. – Shivam Pokhriyal Feb 11 '20 at 12:18
  • Well have you implemented my suggestions? You would have mimetype, filename and extension already. You can determine file size too then. I wonder for what you need a mimetype for copying a file. – blackapps Feb 11 '20 at 12:30
  • Yeah, I'm doing changes to implement your suggestion only. I need mimetype to sort of check if the app is supporting that particular mimeType. PS: This only happens when the file is selected using Google Photos in emulator(API level 29). If I select the same file by navigating through directories, it works perfectly fine. So, I'm guessing this particular bug is related to Google Photos app. – Shivam Pokhriyal Feb 11 '20 at 12:35
  • Yes think too that its related to the provider. They are different then. You can see it in the obtained content scheme. – blackapps Feb 11 '20 at 12:36
  • Yup the providers are totally different. I'll edit the post to include that detail as well in few minutes. And will confirm if your suggestion works or not in maybe 10 minutes. – Shivam Pokhriyal Feb 11 '20 at 12:38
  • I tried using the MediaStore.MediaColumns.MIMETYPE thing, but it's throwing IllegalArgumentException to me with message Invalid column latitude. I've only added MediaStore.MediaColumns.MIME_TYPE column in the query. This is the code:: context.getContentResolver().query(uri, new String[] {MediaStore.MediaColumns.MIME_TYPE}, null, null, null); – Shivam Pokhriyal Feb 11 '20 at 12:58
  • No. Do it different: Cursor cursor = context.getContentResolver().query(uri, null, null, null); And then use the right column on the cursor. – blackapps Feb 11 '20 at 13:00
  • @blackapps It's insane. I'm getting same IllegalArgumentException when using null projection. Same happens when I use _id. This is so weird, I guess either the Google Photos app in emulator is buggy or there is some change in API level 29 which I can't seem to figure out. – Shivam Pokhriyal Feb 12 '20 at 05:38
  • I do it in Pixel 3 XL emulator in this way. Android Q. Please post complete onActivityResult code in your post. – blackapps Feb 12 '20 at 08:53
  • @blackapps I've created a sample here https://github.com/ShivamPokhriyal/BuggyGooglePhotos containing the minimal code, Just 100 lines probably. Also asked a new question with more screenshots if that helps get better idea of the issue https://stackoverflow.com/questions/60182338/selecting-video-from-google-photos-provider-doesnt-give-mimetype-information – Shivam Pokhriyal Feb 13 '20 at 09:14
  • 100 lines is much to much. Please post your onActivityResult code here. Omit all irrelevant stuff. – blackapps Feb 13 '20 at 10:03
  • Is it possible for you to check the other stackoverflow question linked in above comment or you want me to paste the same thing here as well? I'm starting to feel really bad about the way things are working at stackoverflow. Some people suggest to ask a new question and some say to edit the same post. And some day later someone will come and mark the other question as duplicate. Really insane. When all you have to do is click a link and read the question with all the details like screenshots, code and links. But you want me to copy and paste the same thing over here again. – Shivam Pokhriyal Feb 13 '20 at 10:10
  • No offence, I would've edited the same post, but I remember someone told me to ask a new question. – Shivam Pokhriyal Feb 13 '20 at 10:15
  • I ask you for the third time to post your onActivityResult() code. You do not need more then twenty lines. If you have more you have irrelevant code lines. And do not post screenshots of text. Post text. – blackapps Feb 13 '20 at 11:53

0 Answers0