【问题标题】:Selecting video from Google Photos provider doesn't give mimetype information从 Google 照片提供商选择视频不会提供 mimetype 信息
【发布时间】:2020-05-27 15:45:32
【问题描述】:

我创建了一个示例here 来重现此问题。 我在一个 API 级别为 29 的模拟器上运行这个项目,并从谷歌照片提供商那里选择一个视频。 但我不知道如何获取所选文件的 mimeType。 我使用了 ContentResolver 的 getType 方法,它返回 null 和 ContentResolver 查询方法,它以某种方式向我抛出 IllegalArgumentException。我已经附上了相同的屏幕截图。

注意:如果我通过浏览目录选择相同的文件,一切正常,但是当我使用谷歌照片提供程序时,它会失败。

这是我的代码::

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 + "");
            }
        }
    }

这就是我开始意图的方式::

Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/*");
startActivityForResult(i, IMAGE_VIDEO_FETCH);

谁能帮助我找出所选文件的 mimetype 的正确方法? PS:我试过 ContentResolver.getType、MimeTypeMap 和 ContentResolver.query。它们都不起作用。

【问题讨论】:

  • 你能提供一个示例 URI 吗?
  • content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/643532680
  • 我尝试了相同的代码,从“Google 相册”中挑选了一个视频,我得到的 mime 类型为“video/mp4”。您是否确认视频已损坏?

标签: android mime-types google-photos google-photos-api


【解决方案1】:

这是我用来获取 MimeType 的: 不幸的是,它在 Kotlin 中,但让我们尝试使用 Kotlin,效果会更好。

 @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
}

希望对你有所帮助!

【讨论】:

  • 嘿,您是否检查了问题的最后一部分,我提到内容解析器 getType 不起作用。而且 Photos porvider 只提供 ContentSceheme。
【解决方案2】:

我浪费了几天时间试图找出问题所在。不幸的是,它原来是谷歌照片 4.17 版中的一个错误,似乎已在 4.32 版中修复

这是链接的谷歌问题https://issuetracker.google.com/issues/149416521

【讨论】:

  • 非常感谢您的调查并指出确切的问题。即使我也花了很多时间和天来解决这个问题,但我也有同样的问题,我检查了谷歌照片的版本,它是 4.17。非常感谢您的努力。
  • 真的很高兴它有帮助!
猜你喜欢
  • 2012-09-22
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-16
  • 2013-03-27
相关资源
最近更新 更多