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

百般思念 提交于 2020-02-23 10:09:14

问题


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.


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/60182338/selecting-video-from-google-photos-provider-doesnt-give-mimetype-information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!