2014-09-22 87 views
0

Uri返回目錄路徑,我需要實現獲取音頻文件的代碼並在MediaPlayer上播放該音頻。我認爲MediaPlayer需要絕對路徑。如何從android URI獲取絕對音頻路徑

Intent intent; 
    intent = new Intent(); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    intent.setType("audio/*"); 
    startActivityForResult(Intent.createChooser(intent,"Select Audio"), 3); 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     // TODO Auto-generated method stub 
     super.onActivityResult(requestCode, resultCode, data); 
    if((requestCode == 3) && (resultCode == RESULT_OK) && (data != null)){ 
      if ((data != null) && (data.getData() != null)){ 
       //this code returns content directory path 
       String getAudioPath=data.getData().toString(); 

      } 
     } 
    }; 
Log: 

09-22 14:55:50.361: E/AndroidRuntime(9396): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/audio:5260 flg=0x1 }} to activity {com.appwacky.irant/com.test.test.cameraApplication.VideoCapture_New}: java.lang.IllegalArgumentException: URI is not absolute: /document/audio:5260 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3432) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3475) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread.access$1300(ActivityThread.java:139) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1258) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.os.Handler.dispatchMessage(Handler.java:102) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.os.Looper.loop(Looper.java:136) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread.main(ActivityThread.java:5086) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at java.lang.reflect.Method.invokeNative(Native Method) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at java.lang.reflect.Method.invoke(Method.java:515) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at dalvik.system.NativeStart.main(Native Method) 
09-22 14:55:50.361: E/AndroidRuntime(9396): Caused by: java.lang.IllegalArgumentException: URI is not absolute: /document/audio:5260 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at java.io.File.checkURI(File.java:220) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at java.io.File.<init>(File.java:177) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at com.test.test.cameraApplication.VideoCapture_New.onActivityResult(VideoCapture_New.java:513) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.Activity.dispatchActivityResult(Activity.java:5446) 
09-22 14:55:50.361: E/AndroidRuntime(9396):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3428) 

回答

-1
//write to use this 
String getAudioPath=data.getData().getPath().toString(); 

//instead of 

String getAudioPath=data.getData().toString(); 
相關問題