12

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 button to open a dialog to ask you how you want to open a file (like to choose a file explorer) and then the user will select an mp3 file (it should be possible to enter both internal memory and external, at least those available to users, like in my Xperia V which has 2 internal partitions, and an SD Card), and when a music file is selected (.wav, .mp3, .acc files) to load its name and file path in my app. How can I do it? I am not providing any code, because that's all I need

Sartheris Stormhammer
  • 2,534
  • 8
  • 37
  • 81
  • https://stackoverflow.com/a/59104787/3141844 + https://github.com/criss721/Android-FileSelector – Criss Nov 29 '19 at 13:53

3 Answers3

21

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

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • what is REQ_CODE_PICK_SOUNDFILE – Sartheris Stormhammer Jan 29 '14 at 11:55
  • It is the request code used both in startActivityForResult and in onActivityResult to help identify which result you are getting (very helpful if you can start different Activities to get different types of result, for example one to select audio files, another to select or take a picture, etc ...). Official documentation : http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) – 2Dee Jan 29 '14 at 13:27
7

To open file explorer, requestCode is just an integer so for sake , pass 1

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*"); // specify "audio/mp3" to filter only mp3 files
startActivityForResult(intent,1);

Setup Media Player,

MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);

Fetch result file from explorer,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 /*check whether you're working on correct request using requestCode , In this case 1*/

    if(requestCode == 1 && resultCode == Activity.RESULT_OK){
        audio = data.getData(); //declared above Uri audio;
        Log.d("media", "onActivityResult: "+audio);
    }

    super.onActivityResult(requestCode, resultCode, data);
}

Start Playing audio,

 player.setDataSource(new FileInputStream(new File(audio.getPath())).getFD());

 player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mediaPlayer) {
                    player.start();
                }
            });

 player.prepareAsync();

To stop audio,

 if(player.isPlaying())
            player.stop();

Finally, Add permission in manifest to read external files,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

Add your controller widgets and play with MediaPlayer methods to create customized Mp3 player ;)

Jegan Babu
  • 1,286
  • 1
  • 15
  • 19
-4

Slight Correction to 2Dee's answer:

    Intent audiofile_chooser_intent;
    audiofile_chooser_intent = new Intent();
    audiofile_chooser_intent.setAction(Intent.ACTION_GET_CONTENT);
    audiofile_chooser_intent.setType("audio/*");
    ActivityOptions options = ActivityOptions.makeScaleUpAnimation(view, 0,
            0, view.getWidth(), view.getHeight());
    startActivityForResult(Intent.createChooser(audiofile_chooser_intent, getString(R.string.select_audio_file_title)), RQS_OPEN_AUDIO_MP3, options.toBundle());

    Toast("File Chooser initiated..");

Intent inside of OnClick listener method to start FileChooser Activity/Dialog

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == RQS_OPEN_AUDIO_MP3) {
            Uri audioFileUri = data.getData();

            String MP3Path = audioFileUri.getPath();
            Toast(MP3Path);


        }
    }
}

OnActivityResult method for obtaining the FilePath of selected file for subsequent use

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Meka
  • 1
  • 1
  • 2
    Let me get this right : you "correction" is to remove the mpeg file extension from the file type to choose ? How does that correct my answer ? How does that improve it ? Do you even have any idea what the code you posted does ? Why did you add ActivityOptions.makeScaleUpAnimation and why is it necessary to select an mp3 file ? So many questions, and I'll probably never get an answer. Ah, the joys of SO :-) – 2Dee May 23 '16 at 14:00
  • It took you a year to notice this :P @2Dee – grantespo Jun 04 '17 at 22:06