在Android中,如何显示文件类型为基础的应用程序选择?文件类型、应用程序、基础、Android

2023-09-07 08:38:02 作者:年轻要闯.

道歉,如果这已经回答了 - 如果有人可以点我到一个已经回答了问题,那将是巨大的......

Apologies if this has already been answered - if someone can point me to an already-answered question, that would be great...

其实很简单,我希望能够弹出的能够处理特定类型的文件的应用程序列表 - 例如,如果我有一个音乐文件(MP3,OGG,等等),我想到能够显示的所有加载的媒体应用程序可以处理该文件,并允许用户在列表中选择一个。

Very simply, I would like to be able to pop up a list of applications which can process a file of a given type - for instance, if I have a music file (mp3, ogg, whatever), I'd like to be able to display a list of all loaded media applications that can process the file and allow the user to select one.

我一直在玩弄创建一个意向要做到这一点,如下所示:

I've been playing around with creating an intent to do this as follows:

Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.addCategory("android.intent.category.LAUNCHER");
myIntent.setType("mp3");
startActivity(myIntent);

但我显然得到它错了,因为它崩溃每次。我应该使用ACTION_VIEW的ACTION_PICK或ACTION_CHOOSER呢?

but I've obviously gotten it wrong, since it crashes every time. Should I be using ACTION_PICK or ACTION_CHOOSER instead of ACTION_VIEW?

更重要的是,是否有可能通过比Intent.type其他的东西很容易地定义所有的媒体播放器?我认为,类型应该是一个正确的MIME类型,在较低的情况下,但我不知道在这种情况下使用什么...

More importantly, is it possible to easily define all media players by something other than the Intent.type? I believe type should be a proper MIME type, in lower case, but I'm not sure what to use in this instance...

请温柔:)

推荐答案

要获得可以成功地打开一个给定的意图,你将使用PackageManager的应用程序。简单地构造意图如上然后用这个code,以获得可以处理这个意图的应用程序。

To get the applications that can successfully open a given intent you'll use the PackageManager. Simply construct the intent as above and then use this code to get the applications that can handle the intent.

Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.addCategory("android.intent.category.LAUNCHER");
myIntent.setType("mp3");    

PackageManager manager = getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(
    myIntent, PackageManager.MATCH_DEFAULT_ONLY);

这将会给你,可以处理的意图,包括图标,包名程序的所有信息。然后,您可以创建具有这些选项的对话框,并保存用户选择的选项。

This will give you all the information on the programs that can handle the intent, including icon, and packagename. You can then create a dialog box with these options and save the option the user chooses.

 
精彩推荐