2

I'm trying to run Google Vision FaceTracker but I have an error on one line of code in CameraSourcePreview.

This is the error - Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException

And this is the function-

private void startIfReady() throws IOException {
    if (mStartRequested && mSurfaceAvailable) {

        mCameraSource.start(mSurfaceView.getHolder()); //Error

        //...other code

        mStartRequested = false;
    }
}
fraiser
  • 929
  • 12
  • 28
  • Hi. Have a look at what permissions FaceTracker needs and then request them by doing int permission = checkPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE); (My example is for file writing, yours will probably be for something else). The check permission method looks like this int permissionResult = ActivityCompat.checkSelfPermission(context, permission); – Smashing Apr 21 '17 at 04:59
  • Thanks! Fixed the issue, but the app when I run it, just gives me a partially black screen :( – fraiser Apr 21 '17 at 05:37
  • Have you also checked CAMERA permission ? – Lubos Mudrak Apr 21 '17 at 06:16
  • Yes, I have. I'm also allowing it when I start the app. – fraiser Apr 21 '17 at 06:21
  • Can I add it as the answer?.. Are there any errors for the black screen? Its really weird – Smashing Apr 21 '17 at 08:03
  • I guess you could. No errors. Here's a question I posted - http://stackoverflow.com/questions/43534783/google-vision-facetracker-shows-a-black-screen – fraiser Apr 21 '17 at 08:17

1 Answers1

0

It does seem that you are not getting the correct permissions that FaceTracker needs. For your needs just add the facetracker permissions into the below implementation and interface.

In our app we added an interface with a callback which handles all our permissions for us :

public interface PermissionAndPackageAvailabilityChecker {

    void checkCameraPermission(final PermissionResult callback);

    void checkFileIoPermission(final PermissionResult callback);

    interface PermissionResult {
        void hasPermission();

        void noPermission();
    }
}

The implementation is pretty simple :

public class DefaultPermissionAndPackage implements PermissionAndPackageAvailabilityChecker {

    private final Context mContext;

    @Inject
    public DefaultPermissionAndPackage(Context context) {
        mContext = context;
    }

    @Override
    public void checkCameraPermission(final PermissionResult callback) {
        int cameraPermission = checkPermission(mContext, Manifest.permission.CAMERA);
        if (checkHasPermission(cameraPermission)) {
            callback.hasPermission();
        } else {
            callback.noPermission();
        }
    }

    @Override
    public void checkFileIoPermission(PermissionResult callback) {
        // Check if we have write permission
        int permission = checkPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        if (checkHasPermission(permission)) {
            callback.hasPermission();
        } else {
            callback.noPermission();
        }
    }

    private int getTargetSdk(int defaultVersion) {
        int targetSdkVersion = defaultVersion;
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            targetSdkVersion = packageInfo.applicationInfo.targetSdkVersion;

        } catch (PackageManager.NameNotFoundException ignored) {
            //Should not happen . . . I hope
        }
        return targetSdkVersion;
    }

    private int checkPermission(final Context context, final String permission) {
        int permissionResult = ActivityCompat.checkSelfPermission(context, permission);
        // this can probably be simplified but explains the logic around permissions nicely.
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1
                && getTargetSdk(Build.VERSION_CODES.LOLLIPOP_MR1) <= Build.VERSION_CODES.LOLLIPOP_MR1) {
            permissionResult = PermissionChecker.checkSelfPermission(context, permission);
            //Will check marshmallow here in the future
        }
        return permissionResult;
    }

    private boolean checkHasPermission(int permissionToCheck) {
        return permissionToCheck == PackageManager.PERMISSION_GRANTED;
    }
}
Smashing
  • 1,640
  • 18
  • 16