如何打开音乐选择器? [英] How to open music picker?
问题描述
在我的应用程序中,我想选择一个可以使用户选择音乐的选择器.我想使用本地android选择器.我使用以下代码打开了本地android音乐选择器:
In my application, I want to make a picker that offers the user the choice to pick a music. I want to use the native android picker. I used the following code to open the native android music picker:
final Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);
但是当我执行它时,我得到一个ActivityNotFoundException和以下错误消息:
But when I execute it, I get an ActivityNotFoundException and this error message:
您的手机没有可用于选择文件的音乐库.请尝试发送其他类型的文件"
"Your phone has no music gallery that can be used to select a file. Please try sending a different type of files"
我在那做错了吗?
推荐答案
这对我来说效果很好:
public static final int REQ_PICK_AUDIO = 10001;
//------
Intent audio_picker_intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
activity.startActivityForResult(audio_picker_intent, REQ_PICK_AUDIO);
更通用的Intent.ACTION_GET_CONTENT可以为用户提供许多选择活动的选项,以选择音频文件(Astro文件管理器等).但是,用户可以选择任何文件,而不必选择音频文件.我想要一个仅允许用户从其媒体中选择音频文件的文件.这就是窍门.
The more general intent with Intent.ACTION_GET_CONTENT can present the user with a number of options for activities to pick an audio file (Astro file manager, etc.). However, the user could select any file, not necessarily an audio file. I wanted one that simply allowed the user to select an audio file from their media. This did the trick.
这篇关于如何打开音乐选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!