lokoko
lokoko

Reputation: 5803

How can i get the Audio file that the user picks

I would want to get a handle to the actual audio file that i pick. For selecting the audio file, i have an intent that looks like this :

Intent audioIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(audioIntent , 0);

It allows me to get a popup to pick audio files from but how can i get the file that i have picked ?

In the case of images, i have :

bitmap = android.provider.MediaStore.Images.Media.getBitmap(getContentResolver(), intent.getData());

Is there something similar for audio files ?

Upvotes: 0

Views: 3251

Answers (2)

lokoko
lokoko

Reputation: 5803

Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, AUDIO_GALLERY_REQUEST_CODE);

if (requestCode == AUDIO_GALLERY_REQUEST_CODE) {
    if (resultCode == Activity.RESULT_OK) {
        try {
          String path = _getRealPathFromURI(context, intent.getData());
          File audio = new File(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

private String _getRealPathFromURI(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Audio.Media.DATA };
    CursorLoader loader = new CursorLoader(context, contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Upvotes: 5

drRoflol
drRoflol

Reputation: 305

There should be a similar method for android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, it has a getContentUri(String) that returns a Uri object. See if that helps.

Link to Android reference

Upvotes: 0

Related Questions