onActivityResult RESULT_OK can not be resolved to a variable in android?

AndroidAndroid Fragments

Android Problem Overview

I am trying to launch camera in fragment but onActivityResult in fragment doesn't resolve RESULT_OK. What should i do?

I am launching camera using:

public static final int CAMERA_REQUEST_CODE = 1999;

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);

get captured image using:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
		Bitmap bitmap = (Bitmap) data.getExtras().get("data");
		if (bitmap != null) {

and i want captured image in current fragment!

Android Solutions

Solution 1 - Android

RESULT_OK is constant of Activity class. In Activity class you can access directly but in other classes you need to write class name (Activity) also.

Use Activity.RESULT_OK instead of RESULT_OK.

In your case it will be

if (requestCode == CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

Solution 2 - Android

In fragment we must use getActivity() method as prefix with RESULT_OK.

In your case it will be:-

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

Solution 3 - Android

Alternatively you can add import static android.app.Activity.RESULT_OK; and use it in your case like if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {..}


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTulsiram RathodView Question on Stackoverflow
Solution 1 - AndroidPankaj KumarView Answer on Stackoverflow
Solution 2 - AndroidEkta BhawsarView Answer on Stackoverflow
Solution 3 - AndroidFivosView Answer on Stackoverflow