如何从Android URI获取绝对音频路径

时间:2014-09-22 09:07:43

标签: android audio android-mediaplayer absolute-path

Uri返回目录路径,我需要实现一个代码来获取音频文件并在MediaPlayer上播放该音频。我认为MediaPlayer需要绝对的路径。

 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 个答案:

答案 0 :(得分:-2)

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

//instead of 

 String getAudioPath=data.getData().toString();