Shi*_*yal 6 android mime-types google-photos google-photos-api
我在这里创建了一个示例来重现这个问题。我在 API 级别为 29 的模拟器上运行这个项目,并从谷歌照片提供商处选择一个视频。
但我无法弄清楚如何获取所选文件的 mimeType。
我使用了 ContentResolver 的 getType 方法,它返回 null 和 ContentResolver 查询方法,它以某种方式向我抛出 IllegalArgumentException。我附上了一张截图。

注意:如果我通过浏览目录选择相同的文件,一切正常,但是当我使用 google 照片提供程序时,它会失败。
这是我的代码::
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 + "");
}
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我开始意图的方式:
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/*");
startActivityForResult(i, IMAGE_VIDEO_FETCH);
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我找出所选文件的 mimetype 的正确方法吗? PS:我试过 ContentResolver.getType、MimeTypeMap 和 ContentResolver.query。它们都不起作用。
我浪费了几天时间试图找出问题所在。不幸的是,它被证明是谷歌照片版本 4.17 中的一个错误,似乎在版本 4.32 中得到了修复
这是链接的谷歌问题https://issuetracker.google.com/issues/149416521
| 归档时间: |
|
| 查看次数: |
312 次 |
| 最近记录: |