1

I have made an Application in which I want to pick a song from sd card and the selected song's path should be returned back to onActivityResult() so that I can do bla bla bla..How can I get that?Any help is appreciated.It's really important for me.

Code:

@Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        menu.add("Pick Song");
        return super.onCreateOptionsMenu(menu);
    }

    protected File getTempFile()
    {
        dir =new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Music");
        return dir;
     } 

    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        super.onOptionsItemSelected(item);
        //System.gc();
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("audio/mp3");
        Uri data = Uri.fromFile(getTempFile());
        intent.setData(data);
        startActivityForResult(intent, Pick_song);
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);

        switch(requestCode)
        {
            case Pick_song : if ((resultCode == RESULT_OK)&&(data != null))
            { 
                final String ringTonePath = data.getData().getEncodedPath();
                Button playbutton = (Button)findViewById(R.id.play);
                playbutton.setVisibility(0);
                playbutton.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View view)
                    {
                        try 
                        {
                            mMediaPlayer.setDataSource(ringTonePath);
                            mMediaPlayer.start();
                            mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
                            {
                                @Override
                                public void onCompletion(MediaPlayer mp) 
                                {
                                    mp.release();
                                    mp = null;                                   
                                }
                            });                             
                        }
                        catch (Exception exception) 
                        {
                            exception.printStackTrace();
                        }                       
                    }
                });
            }
        }
    }

UPDATE:I an getting NUll pointer Excepton in setDataSource().as I am getting content for that song not a URI.thats y i got NPE.How to resolve that?

Geetanjali
  • 1,043
  • 3
  • 13
  • 37
  • If u want us to know the error u should post ur logcat details :), better check in the manifest whether u have declared the activitys name or not(if u have another activity) coz of which ur getting the error.am I right? – Randroid Aug 16 '11 at 13:01
  • I got error in setAction(Intent.ACTION_GET_CONTENT); should be used instead of Intent.PICK – Geetanjali Aug 16 '11 at 13:18

2 Answers2

1

I got the answer

code:

  System.gc();
  mMediaPlayer = new MediaPlayer();
  if(mMediaPlayer.isPlaying())
  {
        mMediaPlayer.reset();
  }
  String[] proj = { MediaStore.Images.Media.DATA };
  musiccursor = managedQuery(mUri, proj, null, null, null);
  music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
  musiccursor.moveToFirst();
  String filename = musiccursor.getString(music_column_index);
  mMediaPlayer.setDataSource(filename);
  mMediaPlayer.prepare();
  mMediaPlayer.start();
  mMediaPlayer.setOnCompletionListener(new OnCompletionListener() 
  {
            @Override
            public void onCompletion(MediaPlayer mp) 
            {
                mp.release();
                mp = null;                                   
        }
  });       

By this we can easily convert content uri to file uri.

Kartik Domadiya
  • 29,868
  • 19
  • 93
  • 104
Geetanjali
  • 1,043
  • 3
  • 13
  • 37
0

According to Using Intent.ACTION_PICK for specific path, it seems that you cannot specify a directory using ACTION_PICK. However, you can easily accomplish what you want if you circumvent ACTION_PIC & simply implement a browse dialog. For help regarding implementing a browse dialog, have a look at Choose File Dialog.

HTH,

Akshay

Community
  • 1
  • 1
Akshay
  • 5,747
  • 3
  • 23
  • 35