确定意图返回的android中的uri的Mime类型? [英] Determine Mime type of a uri in android returned by an intent?

查看:459
本文介绍了确定意图返回的android中的uri的Mime类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先是我要实现的目标的上下文-

First some context of what I am trying to achieve -

我正在开发一个图像共享应用程序,需要用户从文件系统中选择一个图像.

I am developing an Image Sharing application for which I need the user to pick an image from the filesystem .

为此,我正在使用此代码-

For this , I am using this code -

    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, REQUEST_CODE_IMAGE_PICKER_INTENT);

现在,这会触发一个选择器,在该选择器中,我会看到多个选项,包括手机中具有的Gallery(画廊)和FileManager App.如果我选择图库并从中选择一张图片,那么我的活动将收到一个包含图片内容uri的Intent. 但是,如果选择FileManager App,则可以选择任何可能不是图像的文件.所以,我需要的是能够确定意图中返回的uri的mime类型.意图的getType方法返回null.

Now , this triggers a chooser wherein I see multiple options including Gallery and a FileManager App that I have in my phone. If i select gallery and choose an image from there then my Activity receives an Intent with a content uri of an Image. But if I choose the FileManager App , I can choose any file which might not be an image . So , what I need is to be able to determine the mime type of the uri returned in the intent. The intent's getType method returns null .

有什么方法可以从返回的意图中确定哑剧,从而能够确定内容是否为图像.

Is there any way to determine the mime from the returned intent to be able to determine if the content is an image or not .

如果这种方法不能解决问题,那么我可能必须使用MimeTypeMap从文件扩展名中确定mime.

If this doesn't work this way , then I might have to use MimeTypeMap to determine the mime from the file extension.

推荐答案

我找到了一种获取内容uri的mime类型的方法,但对其他类型的uri(例如"file://"形式的uri)无效. ....'.

I found a way to get the mime type of a content uri but nothing worked for other kind of uri's such as uri's of the form 'file://.....' .

要获取内容uri的MIME类型-

To get the mime type of content uri's -

    ContentResolver cr = this.getContentResolver();
    String mime = cr.getType(YOUR_CONTENT_URI);

这仅适用于内容uri.因此,对于其他uri,我正在使用MimeTypeMap从文件扩展名推断其mime类型.

This works only for content uri's . So for other uri's I am using MimeTypeMap to infer their mime type's from the file extension .

这篇关于确定意图返回的android中的uri的Mime类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆