0

Here's what the app does - User clicks button (to take a picture) on Activity A, the captured image gets set as on an ImageView in Activity A, and then the user clicks a "save" button, which takes him/her to Activity B, where the image they took gets displayed (on an ImageView in Activity B)

In order to do this I am trying to find a way to save the image. I tried many different things, but I keep getting a NullPointerException. Here is my code:

I highlighted the area where I think the error is:

 public void onClick(View view) {
    switch (view.getId()) {
        case R.id.take_picture_button:
            takePic = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePic.resolveActivity(getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile(); //so photoFile = the file we created up top
                } catch (IOException ex) {
                    // Error occurred while creating the File
                    ex.printStackTrace();

                }
                // Continue only if the File was successfully created
                if (photoFile != null) {

                    **takePic.putExtra(MediaStore.EXTRA_OUTPUT**, //extra_output is just the name of the Intent-extra used to indicate a content resolver Uri to be used to store the requested image or video.
                            **Uri.fromFile(photoFile));**
                    startActivityForResult(takePic, cameraData);
                }

And here is the code for the onActivityResult

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == cameraData && resultCode == RESULT_OK) {
        mReturningWithResult = true;
        extras = data.getExtras();

And here is where I set my image view to my captured image (on Activity A)

@Override
protected void onPostResume() {
    super.onPostResume();
    if (mReturningWithResult) {
        foodImage = (Bitmap) extras.get("data");
        foodImageView.setImageBitmap(foodImage);
    }
    mReturningWithResult = false;//resetting it for next time

}

Here is the logcat (sorry I'm new to this)

    10-15 20:58:59.612  32005-32005/com.example.nikhil.foodshark D/OpenGLRenderer﹕ Enabling debug mode    0
10-15 20:59:21.545  32005-32005/com.example.nikhil.foodshark I/PersonaManager﹕ getPersonaService() name persona_policy
10-15 20:59:23.127  32005-32005/com.example.nikhil.foodshark W/IInputConnectionWrapper﹕ showStatusIcon on inactive InputConnection
10-15 20:59:33.678  32005-32005/com.example.nikhil.foodshark D/AndroidRuntime﹕ Shutting down VM
10-15 20:59:33.678  32005-32005/com.example.nikhil.foodshark W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41a39da0)
10-15 20:59:33.678  32005-32005/com.example.nikhil.foodshark E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.nikhil.foodshark, PID: 32005
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.nikhil.foodshark/com.example.nikhil.foodshark.NewDish}: java.lang.NullPointerException
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3680)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723)
            at android.app.ActivityThread.access$1400(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.nikhil.foodshark.NewDish.onActivityResult(NewDish.java:144)
            at android.app.Activity.dispatchActivityResult(Activity.java:5650)
            at android.app.ActivityThread.deliverResults(ActivityThread.java:3676)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3723)
            at android.app.ActivityThread.access$1400(ActivityThread.java:174)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5593)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)
10-15 20:59:56.353  32005-32005/com.example.nikhil.foodshark I/Process﹕ Sending signal. PID: 32005 SIG: 9
10-15 20:59:56.613      378-378/com.example.nikhil.foodshark I/PersonaManager﹕ getPersonaService() name persona_policy
10-15 20:59:56.723      378-378/com.example.nikhil.foodshark I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050_msm8960_refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050__release_AU ()
    OpenGL ES Shader Compiler Version: 17.01.12.SPL
    Build Date: 03/28/14 Fri
    Local Branch:
    Remote Branch: refs/tags/AU_LINUX_ANDROID_KK_2.7_RB1.04.04.02.007.050
    Local Patches: NONE
    Reconstruct Branch: NOTHING
10-15 20:59:56.763      378-378/com.example.nikhil.foodshark D/OpenGLRenderer﹕ Enabling debug mode 0

Do you guys know a way I can fix this? Thank you!

Red Ghost
  • 302
  • 2
  • 12

1 Answers1

0

You are passing MediaStore.EXTRA_OUTPUT, in which case, the intent field in onActivityResult can be null.

The solution is to just use the photo file you created, and passed as uri in the putExtra

So instead of trying to get the photo file location from the intent, just use Uri.fromFile(photoFile)

replace

    extras = data.getExtras();
    ...

with

   `Uri.fromFile(photoFile)`

or just use photoFile if that is what you want...just dont rely on intent data

ashoke
  • 6,441
  • 2
  • 26
  • 25