开发者

Android;Problem in Pick song from SD card and return path

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.onActivityRes开发者_如何转开发ult(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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜