2011-08-16 64 views
1

我已經制作了一個應用程序,我想從SD卡中選擇一首歌曲,並將所選歌曲的路徑返回到onActivityResult(),以便我可以做bla bla bla ..我怎麼得到?任何幫助表示讚賞。這對我來說非常重要。Android;從SD卡挑選歌曲和返回路徑中的問題

代碼:

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

更新:我得到一個空指針Excepton中的setDataSource(),因爲我爲這首歌獲得內容不是URI.thatsŸ我得到NPE.How來解決呢?

+0

如果你希望我們知道錯誤你應該發佈你的logcat詳細信息:),最好檢查清單是否你已經宣佈活動名稱或沒有(如果你有另一個活動)因爲你得到錯誤。我對嗎? – Randroid

+0

我在setAction(Intent.ACTION_GET_CONTENT)中出錯;應該使用,而不是Intent.PICK – Geetanjali

回答

1

我得到了答案

代碼:

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

通過這一點,我們可以輕鬆地將內容URI到文件URI。