问题描述
我开发,其中列出文件夹中的文件的应用程序(在ListView)。当用户点击其中一个项目,如果它是一个文件,然后我想,推出了可以处理它,如果有的话,或显示某种错误消息,如果是没有的一种活动。
I am developing an application which lists files in a folder (in a ListView). When the user clicks on one of the items, if it is a file, then I would like to launch an activity that can handle it, if any, or display some kind of error message if there is none.
我怎样才能做到这一点?不是整个事情,当然,但我怎么能确定哪个应用程序(S)可以处理一个文件(如有)。
How can I do that? Not the whole thing, of course, but how can I determine which application(s) can handle a file, if any.
推荐答案
首先,你需要确定MIME类型的文件。为此,您可以使用:
First, you will need to determine the MIME type of the file. You can do this using MimeTypeMap:
MimeTypeMap map = MimeTypeMap.getSingleton();
String extension = map.getFileExtensionFromUrl(url); // url is the url/location of your file
String type = map.getMimeTypeFromExtension(extension);
然后,一旦你知道的MIME类型,您可以创建该类型的意图:
Then once you know the MIME type, you can create an intent for that type:
Intent intent = new Intent();
intent.setType(type);
最后,要检查是否有任何活动,能够解决与该类型的意图。这可以通过PackageManager:
PackageManager manager = getPackageManager(); // I'm assuming this is done from within an activity. This a Context method.
List<ResolveInfo> resolvers = manager.queryIntentActivities(intent, 0);
if (resolvers.isEmpty()) {
// display error
} else {
// launch the intent. You will also want to set the data based on the uri of your file
}
这篇关于推出基于android系统中的文件的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!