处理从新Google相册应用中选择的文件的正确方法

问题描述 投票:0回答:1

Choosing photo using new Google Photos app is broken不重复

我的应用程序请求的文件可以是图像或视频,我们仅支持某些文件扩展名。由于不建议使用MediaStore.MediaColumns.Data,因此我必须采用fileDescriptor方法来解决此问题。 但是我无法找到一种方法来获取所选文件的扩展名。

从Google相册应用中选择视频后获得的URI如下:

content://com.google.android.apps.photos.contentprovider/-1/2/content%3A%2F%2Fmedia%2Fexternal%2Fvideo%2Fmedia%2F25/ORIGINAL/NONE/643532680

[当我尝试使用_data列查询ContentResolver时,它为我提供了IllegalArgumentException,因此我使用下面的代码将文件复制到一个临时位置。

String[] str = uri.toString().split("/");
String fileName = str[str.length - 1];
String filePath = context.getFilesDir().getAbsoluteFile() + File.separator + fileName;
File newFile = new File(filePath);
FileDescriptor fileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r").getFileDescriptor();
FileInputStream fis = new FileInputStream(fileDescriptor);
copyFile(fis, newFile);
return filePath;

所以,我应该如何获得所选文件的正确mimeType?

PS:我尝试了MimeTypeMap,ContentResolver.getType,但是它们都不起作用。

android mime-types
1个回答
0
投票

但是我无法找到一种方法来获取所选文件的扩展名。

用户选择了一条内容。不需要是文件,也没有文件扩展名。

我应该如何获得所选文件的正确mimeType?

getType()上调用ContentResolver,传入Uri,以获得与Uri关联的内容的MIME类型。

PS:我尝试了MimeTypeMap,ContentResolver.getType,但是它们都不起作用。

然后可能会问一个单独的堆栈溢出问题,您在其中提供minimal, reproducible example的用法getType()。您的代码中有错误或者Google相册中有错误。

© www.soinside.com 2019 - 2024. All rights reserved.