有意向的音頻文件選擇它做工精細,正確的,但是當我選擇從文件管理器應用程序崩潰的MP3文件,並給我下面的日誌:未能提供結果ResultInfo時挑文件是從文件管理器
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=file:///mnt/sdcard/Download/g.p.mp3 }} to activity {com.example.er.part2/com.example.er.part2.Main2Activity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.er.part2.Main2Activity.getRealPathFromURI(Main2Activity.java:128)
at com.example.er.part2.Main2Activity.onActivityResult(Main2Activity.java:98)
at android.app.Activity.dispatchActivityResult(Activity.java:5192)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
at android.app.ActivityThread.access$1100(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
這是我的代碼:
Button button=(Button)findViewById(R.id.select);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent_upload = new Intent();
intent_upload.setType("audio/*");
intent_upload.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent_upload,1);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == Activity.RESULT_OK){
if ((data != null) && (data.getData() != null)){
audioFileUri = data.getData();
// Now you can use that Uri to get the file path, or upload it, ...
filePath = getRealPathFromURI(getApplicationContext(),audioFileUri);
}
}
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Audio.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
正如我告訴當從媒體播放器的選擇,但是當從它發生的文件管理器選擇它做工精細!以下從日誌
Caused by: java.lang.NullPointerException at com.example.er.part2.Main2Activity.getRealPathFromURI(Main2Activity.java:128)
點這一行部分:
int column_index = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
你可以添加一些代碼?或任何來源... – Erf
@Erf:從'ACTION_GET_CONTENT'或'ACTION_OPEN_DOCUMENT'使用'Uri'在[文檔](https://developer.android.com/guide/topics/providers/document-provider的.html)和[我的書](https://commonsware.com/Android/previews/consuming-documents),以及其他地方。我也進入這一主題[本博客文章(https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html)。 – CommonsWare
先生,我搜索這個和嘗試一些事情,但仍然無法正常工作!你能告訴我代碼必須改變嗎?我真的需要幫助 – Erf