0

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);
}
ColdFire
  • 6,764
  • 6
  • 35
  • 51
Nicholas Muir
  • 2,897
  • 8
  • 39
  • 89

3 Answers3

0

If you are calling openCamera method in fragment you need to check this

onActivityResult is not being called in Fragment

Ferhat Ergün
  • 135
  • 1
  • 10
0

Remove super from onActivityResult().

Remove this line super.onActivityResult(requestCode, resultCode, data);

super will inherit parent class onActivityResult

Senthil JS
  • 31
  • 6
0

you need to change the onActivityResult() :

   @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
            // Do something after user returned from app settings screen
            openCamera();
        } else 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 : Add the below code to your Activity's onActivityResult() method.

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.yourFragmentId);
        fragment.onActivityResult(requestCode, resultCode, data);

}
Hitesh Sarsava
  • 666
  • 4
  • 15
  • Hi, Thanks can you explain more I am wondering what changes you mean. I can see you add it to the if else but how is this different to having a second if statement? Or is there something I am missing thanks. On activity is not being called at all see how I have the log statement before anything in on activity result that is to test if it is being called – Nicholas Muir Apr 20 '18 at 09:16
  • first of all you checking result code instead checking request code in resultCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE, so need to change this – Hitesh Sarsava Apr 20 '18 at 10:52
  • see i have edited my answer. for that case you need to find fragment and need to call fragment's onActivityResult() method – Hitesh Sarsava Apr 20 '18 at 11:03