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:
@Override 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!
The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result.
It has deprecated startActivityForResult in favour of registerForActivityResult . It was one of the first fundamentals that any Android developer has learned, and the backbone of Android's way of communicating between two components.
We use startActivityForResult() to send and receive data between activities, in almost of our android projects. But recently startActivityForResult() method is deprecated in AndroidX. Android came up with ActivityResultCallback (also called Activity Results API) as an alternative for it.
First you use startActivityForResult() with parameters in the first Activity and if you want to send data from the second Activity to first Activity then pass the value using Intent with the setResult() method and get that data inside the onActivityResult() method in the first Activity .
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) {
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With