0

Im using the following code to let the user select an image from his gallery. But after selecting an img the app crashes.

Intent i = new Intent(
        Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

Heres the logcat of the crash:

04-01 00:58:53.210: E/AndroidRuntime(26970): FATAL EXCEPTION: main
04-01 00:58:53.210: E/AndroidRuntime(26970): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/21931 flg=0x1 }} to activity {de.arvidg.exampleactionbartabs/de.arvidg.exampleactionbartabs.AddNewMission}: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3310)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3353)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.access$1100(ActivityThread.java:145)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1259)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.os.Looper.loop(Looper.java:137)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.main(ActivityThread.java:4978)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at java.lang.reflect.Method.invokeNative(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at java.lang.reflect.Method.invoke(Method.java:511)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at dalvik.system.NativeStart.main(Native Method)
04-01 00:58:53.210: E/AndroidRuntime(26970): Caused by: java.lang.NullPointerException
04-01 00:58:53.210: E/AndroidRuntime(26970):    at de.arvidg.exampleactionbartabs.AddNewMission.onActivityResult(AddNewMission.java:131)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.Activity.dispatchActivityResult(Activity.java:5192)
04-01 00:58:53.210: E/AndroidRuntime(26970):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3306)
04-01 00:58:53.210: E/AndroidRuntime(26970):    ... 11 more

Heres the onActivityResult, in which the exception occurs.

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.chosenpictureview);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        filepath = picturePath;
        thread.setPathToOurFile(picturePath);


    }

}
user2147674
  • 2,319
  • 2
  • 14
  • 17

1 Answers1

0

In onActivityResult() you can also open the selected image from the Uri selectedImage directly (that you got with getData()), instead of using the MediaStore query.


Camera or Gallery:

If you wish to allow the user to select from either the gallery or from the camera, make sure to view this post: Allow user to select camera or gallery for image

Community
  • 1
  • 1
David Manpearl
  • 12,362
  • 8
  • 55
  • 72