【发布时间】:2017-03-05 17:40:49
【问题描述】:
有意图音频文件选择器它工作正常且正确,但是当我从文件管理器应用程序崩溃中选择 mp3 文件并给我以下日志时:
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);
【问题讨论】:
标签: java android android-intent android-cursor mediastore