14

在我的应用程序中,我想制作一个选择器,为用户提供选择音乐的选择。我想使用原生的 android 选择器。我使用以下代码打开原生的 android 音乐选择器:

final Intent intent2 = new Intent(Intent.ACTION_PICK);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);

但是当我执行它时,我得到一个 ActivityNotFoundException 和这个错误消息:

“您的手机没有可用于选择文件的音乐库。请尝试发送其他类型的文件”

我在那里做错了吗?

4

3 回答 3

19

这对我来说效果很好:

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 文件管理器等)。然而,用户可以选择任何文件,不一定是音频文件。我想要一个只允许用户从他们的媒体中选择音频文件的工具。这成功了。

于 2013-06-22T15:56:09.600 回答
7

如果您查看最新核心音乐应用程序的AndroidManifest.xml文件,它可能会对您拥有的选项有所了解。例如:

<activity android:name="com.android.music.MusicPicker"
        android:label="@string/music_picker_title" android:exported="true" >
    <!-- First way to invoke us: someone asks to get content of
         any of the audio types we support. -->
    <intent-filter>
        <action android:name="android.intent.action.GET_CONTENT" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="audio/*"/>
        <data android:mimeType="application/ogg"/>
        <data android:mimeType="application/x-ogg"/>
    </intent-filter>
    <!-- Second way to invoke us: someone asks to pick an item from
         some media Uri. -->
    <intent-filter>
        <action android:name="android.intent.action.PICK" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.OPENABLE" />
        <data android:mimeType="vnd.android.cursor.dir/audio"/>
    </intent-filter>
</activity>

所以基于此,你可以先试试

final Intent intent2 = new Intent(Intent.ACTION_GET_CONTENT);
intent2.setType("audio/*");
startActivityForResult(intent2, 1);

看看它是否符合您的需求。您还可以考虑添加上面示例中提到的类别标志以帮助缩小结果范围(例如OPENABLE,应该过滤到仅可以作为流打开的内容。

于 2012-08-13T17:06:47.203 回答
0

类似的东西可能会起作用

// some Intent that points to whatever you like to play
Intent play = new Intent(Intent.ACTION_VIEW);
play.setData(Uri.fromFile(new File("/path/to/file")));
// create chooser for that intent
try {
    Intent i = Intent.createChooser(play, "Play Music");
    c.startActivity(i);
} catch(ActivityNotFoundException ex) {
    // if no app handles it, do nothing
}
于 2012-08-13T16:57:35.847 回答