I am trying to get to open the camera take an image, store in byte array and then display the image on the fragment.
The camera is opening and taking the picture but when you click the tick it just shows the camera again.
I checked the on activity result and it is not being called. I have all the correct permissions setup.
I have tried the other answers on here but with no success.
Here is how I open the camera in the fragment:
@AfterPermissionGranted(REQUEST_PERMISSIONS_ACTIVITY_REQUEST_CODE)
private void openCamera(){
// start main activity
String [] perms = {Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(getActivity(), perms)){
// Start the Camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
} else {
Log.d("Chex ", "Check did not find permissions starting dialog to explain use of user permissions");
EasyPermissions.requestPermissions(this, "This app needs camera permissions to store reciepts", REQUEST_PERMISSIONS_ACTIVITY_REQUEST_CODE, perms);
}
}
This is my onActivityResult in the fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("Chex ", "On activity results called " + resultCode);
if (resultCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE){
openCamera();
} else{
openCamera();
}
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
transactionImageLocationByteArray = stream.toByteArray();
// Load the image into the imageview
Glide.with(getActivity()).asBitmap().load(transactionImageLocationByteArray).into(addTransactionCameraButon);
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getActivity(), "Image Capture Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Image Capture Failed", Toast.LENGTH_SHORT).show();
}
}
}
EDIT:
Adding on requestPermission:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.d("Chex ", "Permission on request result came back with " + grantResults);
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
EDIT 2:
Added onActivityResult to the Main Activity (the host activity for the fragment) but no change:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}