Android select mp3 file

前端 未结 3 2145
故里飘歌
故里飘歌 2020-12-29 11:06

Sorry if this has already been answered, but I can\'t specify my question very well. I have an activity that needs a song file from your device, and I want it when I press a

3条回答
  •  孤城傲影
    2020-12-29 11:10

    I use the following to select an mp3 file :

    Intent intent;
    intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.setType("audio/mpeg");
    startActivityForResult(Intent.createChooser(intent, getString(R.string.select_audio_file_title)), REQ_CODE_PICK_SOUNDFILE);
    

    and then you can get the Uri back in onActivityResult :

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_CODE_PICK_SOUNDFILE && resultCode == Activity.RESULT_OK){
            if ((data != null) && (data.getData() != null)){
                Uri audioFileUri = data.getData();
                // Now you can use that Uri to get the file path, or upload it, ...
                }
            }
    }
    

    I believe selecting other types of audio files would be a matter of setting a wild card in the MIME type by doing intent.setType("audio/*) although I haven't tested that.

    Please let me know if that works for you ;-)

提交回复
热议问题