14

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);

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"

Am I doing something wrong there ?

Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62
Jean-Simon Vaillant
  • 283
  • 1
  • 5
  • 14

3 Answers3

20

This worked well for me:

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);

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.

Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
Craig B
  • 4,763
  • 1
  • 25
  • 19
  • Excellent, please give example for the file storing – Alaa Eldin Jun 27 '13 at 18:44
  • see complete example here: http://sudhanshuvinodgupta.blogspot.co.il/2012/07/using-intentactionpick.html – Guy Apr 08 '14 at 08:41
  • `Intent.ACTION_GET_CONTENT` is indeed a nice picker... it even lets me open a file on my OneDrive. In Comparison, the `ACTION_PICK` of `Media.EXTERNAL_CONTENT_URI` is more concise... **question:** with ACTION_PICK method, how is it possible to pre-select the currently selected item ? (It shows the picker with nothing already selected) – Someone Somewhere Dec 17 '15 at 14:21
  • @Craig This doesn't show downloaded music in some devices – salmanseifian Aug 13 '18 at 06:05
8

If you take a look at the AndroidManifest.xml file for the latest core Music app, it may shed some light on the options that you have. For example:

<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>

So based on this, you might first try

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

and see if it fits your needs. You may also look at adding the category flags noted in the above example to help narrow down the results (e.g. OPENABLE should filter to only content that can be opened as a stream.

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

Something along the lines of this may work

// 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
}
zapl
  • 63,179
  • 10
  • 123
  • 154