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

2

6

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.

Christenechristening answered 12/2, 2020 at 6:27 Comment(3)
Can you provide an example URI?Huskey
content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/643532680Christenechristening
I tried your same code, picked a video from "Google Photos" and I am getting the mime type as "video/mp4". Did you confirm that the video is corrupted or not?Epigrammatist
C
3

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

Christenechristening answered 21/2, 2020 at 6:13 Comment(0)
P
0

This is what I am using to get MimeType: Unfortunately, it's in Kotlin but let try to use Kotlin, much better.

 @SuppressLint("DefaultLocale")
fun getMimeType(uri: Uri): String {
    val mimeType: String
    mimeType = if (uri.scheme == ContentResolver.SCHEME_CONTENT) {
       // Replace MyApplication.getInstance() by your context
        val cr = MyApplication.getInstance().contentResolver
        cr.getType(uri).toString()
    } else {
        val fileExtension = MimeTypeMap.getFileExtensionFromUrl(
            uri
                .toString()
        )
        MimeTypeMap.getSingleton().getMimeTypeFromExtension(
            fileExtension.toLowerCase()
        ).toString()
    }
    return mimeType
}

I hope this help!

Parkerparkhurst answered 21/2, 2020 at 4:43 Comment(1)
Hey, Did you check the last part of the question where I mentioned that content resolver getType is not working. And Photos porvider gives ContentSceheme only.Christenechristening

© 2022 - 2024 — McMap. All rights reserved.