7

I am trying to get the camera permission from the user before launching my camera activity. onRequestPermissionsResult is never called back after the user presses "Allow" from the permission dialog. Here is my Activity class:

public class ImageCaptureActivity extends AppCompatActivity {

    public static final String TAG = ImageCaptureActivity.class.getSimpleName();

    private static final int REQUEST_CAMERA = 0;
    private static final int REQUEST_CAMERA_PERMISSION = 1;
    private Point mSize;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_capture);

        Display display = getWindowManager().getDefaultDisplay();
        mSize = new Point();
        display.getSize(mSize);

        // Request for Camera Permission
        requestForCameraPermission(findViewById(android.R.id.content));
    }

    /**
     * @param view
     * @brief requestForCameraPermission
     */
    public void requestForCameraPermission(View view) {
        Log.v(TAG, "Requesting Camera Permission");

        final String permission = Manifest.permission.CAMERA;
        if (ContextCompat.checkSelfPermission(ImageCaptureActivity.this, permission)
                != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(ImageCaptureActivity.this, permission)) {
                showPermissionRationaleDialog(getString(R.string.camera_permission_rationale), permission);
            } else {
                requestForPermission(permission);
            }
        } else {
            launch();
        }
    }

    /**
     * @param message
     * @param permission
     * @brief showPermissionRationaleDialog
     */
    private void showPermissionRationaleDialog(final String message, final String permission) {
        new AlertDialog.Builder(ImageCaptureActivity.this)
                .setMessage(message)
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ImageCaptureActivity.this.requestForPermission(permission);
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                })
                .create()
                .show();
    }

    /**
     * @param permission
     * @brief requestForPermission
     */
    private void requestForPermission(final String permission) {
        ActivityCompat.requestPermissions(ImageCaptureActivity.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
    }

    /**
     * @brief launch
     */
    private void launch() {
        Log.v(TAG, "Camera Permission Granted, launching the CameraActivity");
        String documentId = getIntent().getStringExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID);
        Intent startCustomCameraIntent = new Intent(this, CameraActivity.class);
        startCustomCameraIntent.putExtra(IntentNames.INTENT_EXTRA_WIP_DOCUMENT_ID, documentId);
        startActivityForResult(startCustomCameraIntent, REQUEST_CAMERA);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CAMERA_PERMISSION:
                final int numOfRequest = grantResults.length;
                final boolean isGranted = numOfRequest == 1
                        && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                Log.v(TAG, "Camera Permission callback on onRequestPermissionsResult");
                if (isGranted) {
                    launch();
                }
                break;

            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

Here is my App Manifest with the Camera Permission:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.myapp.testpackage">

    <uses-permission android:name="android.permission.CAMERA" />

    <uses-feature
        android:name="android.hardware.camera.front"
        android:required="true" />
    <uses-feature android:name="android.hardware.camera2" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application

My Gradle Dependency:

compile 'com.android.support:appcompat-v7:24.0.0'

compile 'com.android.support:design:24.0.0'

compile 'com.android.support:support-v4:24.0.0'
ssk
  • 9,045
  • 26
  • 96
  • 169
  • Do you have the `CAMERA` permission in the manifest [in the proper location](https://commonsware.com/blog/2015/08/31/hey-where-did-my-permission-go.html)? – CommonsWare Oct 18 '16 at 22:18
  • @CommonsWare I have added my manifest information to the question. Yes, I have the permission. I am able to get the permission working. But the callback is not getting called. – ssk Oct 18 '16 at 22:20

1 Answers1

6

I tried your code myself and everything works just fine. I think that the only thing that can be a problem is the scenario when your activity has these two flags:

 android:noHistory="true"
 android:excludeFromRecents="true"

Here's a guy that is explains the issue: https://stackoverflow.com/a/35772265/5281581

Community
  • 1
  • 1
Alex Kamenkov
  • 891
  • 1
  • 6
  • 16
  • Thanks for trying. Removing android:noHistory="true" worked for me. – ssk Oct 19 '16 at 21:32
  • FLAG_ACTIVITY_NO_HISTORY have follwing feature. [ int FLAG_ACTIVITY_NO_HISTORY If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute. If set, onActivityResult() is never invoked when the current activity starts a new activity which sets a result and finishes. ] If requestPermission() is called, the activity is destroyed. So can't get the result. – inhogo May 18 '17 at 03:07