RNK
RNK

Reputation: 5792

Pass extra intent after capturing image from camera

I want to pass an extra intent after capturing an image from camera to identify which image I captured. For that I have,

int TAKE_PHOTO_CODE = 0;
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);

On response,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PHOTO_CODE && resultCode == EditProfileActivity.RESULT_OK) {
        Log.d("CameraDemo", "Pic saved");
        // Get passed intent here...
        System.out.println("EXTRA INTENT: "+data.getStringExtra("profile_pic"));
    }
}

The above code works fine. I am getting image in my local storage. But, I want to pass one more string intent with it.

Something like,

cameraIntent.putExtra("profile_pic", "true");

And on response side I want to get that intent. But, I am getting nullpointer exception. What is the correct way to pass an intent ?

STACK TRACE

01-09 13:29:36.228  23214-23214/com.example.android.navigationdrawerexample E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.android.navigationdrawerexample, PID: 23214
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {com.example.android.navigationdrawerexample/com.example.android.mtesapp.EditProfileActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3942)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
        at android.app.ActivityThread.access$1300(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        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:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.NullPointerException
        at     com.example.android.mtesapp.EditProfileActivity.onActivityResult(EditProfileActivity.java:316)
        at android.app.Activity.dispatchActivityResult(Activity.java:5535)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:3938)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:3992)
        at android.app.ActivityThread.access$1300(ActivityThread.java:156)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5872)
        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:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 1519

Answers (2)

maniaq
maniaq

Reputation: 185

This answer is for new users searching for the same:

  1. You must declare some public parameters in calling method activity(including onActivityResult) according to your need.

  2. In place of start of camera activity(intent) , after startActivityForResult you must set the public parameters(if these places are not same simply check for class name then by casting of input context you could set the public parameters of the main or parent activity.

  3. In onActivityResult if all rights was ok you must use the parameters(with not null checking) for you goal and then display proper messages for user(s).

In summary

  • filling public parameters of main activity in place of camera intent calling.
  • using the public parameters in onActivityResult of main activity (after taking photo and check for proper request code).

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93542

The intent returned in onActivityResult is not necessarily the same result sent by you to the other Activity- its a new Intent with the results of the Activity. So profile_pic isn't in there because the activity you call doesn't put it there.

If you want to save data like that, you need to do it in your app in a variable somewhere.

Upvotes: 1

Related Questions