[英]Get FIle extension using Android Storage Access Framework
I am using the Storage Access Framework in android for selecting images and uploading in my app.我正在使用 android 中的存储访问框架来选择图像并在我的应用程序中上传。 Before uploading these images to my server, i also process these images (resizing).
在将这些图像上传到我的服务器之前,我还会处理这些图像(调整大小)。 However, in the URI returned by the SAF, there is no extension appended, i don't know how to get extension of the file.
但是,在 SAF 返回的 URI 中,没有附加扩展名,我不知道如何获取文件的扩展名。 I need to differentiate between png and jpeg.
我需要区分 png 和 jpeg。 How can i achieve that?
我怎样才能做到这一点? Example uri returned from SAF -
content://com.android.providers.media.documents/document/image%3A245309
从 SAF 返回的示例 uri -
content://com.android.providers.media.documents/document/image%3A245309
My code is something like this我的代码是这样的
if (resultData != null) {
final ClipData clipData = resultData.getClipData();
int takeFlags = resultData.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
ArrayList<Uri> uris = new ArrayList<Uri>();
if(clipData != null){
for (int i = 0; i < clipData.getItemCount(); i++) {
ClipData.Item item = clipData.getItemAt(i);
Uri dataUri = item.getUri();
if (dataUri != null) {
uris.add(dataUri);
Log.d(TAG, "File URI : " + dataUri.toString());
}
}
}else{
Uri dataUri = resultData.getData();
if (dataUri != null) {
uris.add(dataUri);
}
}
processFiles(uris);
}
I need to differentiate between png and jpeg.
我需要区分 png 和 jpeg。 How can i achieve that?
我怎样才能做到这一点?
Call getType()
on a ContentResolver
, passing in the Uri
. 在
ContentResolver
上调用getType()
,传入Uri
。 This will return the MIME type associated with the content, such as image/png
.这将返回与内容关联的 MIME 类型,例如
image/png
。
Using Simple Storage is much easier:使用简单存储要容易得多:
MediaFile file = new MediaFile(context, uri);
String extension = file.getExtension();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.