这是我的logcat
06-03 17:35:58.603: E/AndroidRuntime(300): FATAL EXCEPTION: main
06-03 17:35:58.603: E/AndroidRuntime(300): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {com.example.fatwallet/com.example.fatwallet.Create_adds_Activity}: java.lang.NullPointerException
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 17:35:58.603: E/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 17:35:58.603: E/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 17:35:58.603: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 17:35:58.603: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 17:35:58.603: E/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-03 17:35:58.603: E/AndroidRuntime(300): Caused by: java.lang.NullPointerException
06-03 17:35:58.603: E/AndroidRuntime(300): at com.example.fatwallet.Create_adds_Activity.onActivityResult(Create_adds_Activity.java:101)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
答案 0 :(得分:2)
简单传递意图首先
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
你将在onActivityResult上获得图片路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
获取完整源代码here