1

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.

rejy11
  • 742
  • 2
  • 12
  • 25
  • Can I know on which android version are you running your code? – keshav kowshik Mar 13 '16 at 17:26
  • The answers on [this question](http://stackoverflow.com/questions/10831562/camera-always-returns-resultcode-as-0) seem like possibilities given what you've told us. – Kevin Krumwiede Mar 13 '16 at 17:27
  • @Keshav1234 compileSdkVersion 23, min is 10 – rejy11 Mar 13 '16 at 17:36
  • @KevinKrumwiede I've looked at this question before. I'm not trying to save the image, only trying to display it. I've also tried changing the launch mode for the activity to singleTask, it doesnt seem to make a difference – rejy11 Mar 13 '16 at 17:42
  • @RhysJones is it not working on all versions? – keshav kowshik Mar 14 '16 at 07:01
  • @Keshav1234 I've even tried creating a new project with a targetsdk version of 17 (same as my phone), but it behaves the same way (resultCode is 0) – rejy11 Mar 14 '16 at 12:17

1 Answers1

0

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

alidrsn
  • 109
  • 1
  • 10
  • I was originally using the static int to pass through '1' but then I just set it to 1 when I was trying to figure out my problem. But I did what you said regardless and still no difference :( thanks though – rejy11 Mar 13 '16 at 17:46