简体   繁体   中英

onActivityResult resultCode is 0

I've seen this question asked many times but none of the answers fit my scenario. I have an app that reads NFC tags and does something with the data. I now want to add camera functionality to the app so that when I press a button, it takes me to the camera, after taking a photo this image should then be displayed. However, the resultCode in onActivityResult always returns 0 instead of 1. I only have 1 activity so I cannot see where my problem is. I have this in my manifest:

<uses-feature android:name="android.hardware.camera" android:required="true"/>

When I press the button that calls the onClick method of launchCamera, it executes this..

public void launchCamera(View view)
{
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, 1);
    Log.i(TAG, "launchCamera");
}

OnPause is then called which only has

super.onPause();

The camera launches, I take a picture normally, then it closes and goes back to my Activity, And on ActivityResult gets called (I know this because of the logcat messages).

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i(TAG, "onActivityResult");

    Log.i(TAG, String.valueOf(resultCode));
    Log.i(TAG, String.valueOf(requestCode));

    if(requestCode == 1 && resultCode == RESULT_OK)
    {
        Bundle extras = data.getExtras();
        Bitmap photo = (Bitmap) extras.get("data");
        imgView.setImageBitmap(photo);
    }
}

But for some unknown reason the resultCode is 0, which I also know from the logcat messages. After this, onResume is then called.

I have no idea why its returning as 0, I cannot find any questions or answers that help me this problem so any input would be great OR if there is an answer to this question which I was unable to find, please point me in that direction, cheers.

try this and see what will happen;

-define a static int inside the class and give it to startactivity as a parameter.

private final static int REQUEST_IMAGE_CAPTURE =1; startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

-and listen the result like this;

if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == getActivity().RESULT_OK)

you can use 'this.RESULT_OK' if you writing this code inside an activity.If it's a fragment use getActivitiy().RESULT_OK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM