How can i get the Audio file that the user picks

假装没事ソ 提交于 2019-12-25 00:16:29

问题


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 ?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/19383118/how-can-i-get-the-audio-file-that-the-user-picks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!