Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting video from Google Photos provider doesn't give mimetype information

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. enter image description here

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.

like image 770
Shivam Pokhriyal Avatar asked Feb 12 '20 06:02

Shivam Pokhriyal


1 Answers

I wasted couple of days trying to figure out where the problem exists. Unfortunately it turned out to be a bug in google photos version 4.17 which seems to be fixed in version 4.32

Here is the linked google issue https://issuetracker.google.com/issues/149416521

like image 72
Shivam Pokhriyal Avatar answered Sep 29 '22 14:09

Shivam Pokhriyal