【发布时间】: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