How to restrict use of third party camera app from

2019-05-30 20:23发布

问题:

I have power cam app which is a third party camera app installed in my device. I am opening camera from my app, when i click on open camera button, it gives me choice of cameras like device camera along with power cam. I want that on clicking the open camera button, device camera should get open, in other words i want to restrict the user from using power cam from my app

回答1:

If you want to run only the official camera, you can use the following intent (based on the official tutorial):

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.setPackage("com.android.camera");
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

Unfortunately, many devices come with custom preinstalled camera apps, and com.android.camera may not be available.

If you want to filter out specific packages that you don't like, you must prepare your own chooser dialog (see example here). You can skip the dialog if you know which package to choose. E.g. it is possible to filter the list to only include "system" packages. But even then, there is no guarantee that there will be only one system package that is registered to fulfill the MediaStore.ACTION_IMAGE_CAPTURE intent.