Carl
Carl

Reputation: 250

Android:my Application is forbidden use camera

there is a problem about android:my android application need to use system camera function.it can work very well,but when it run on lenovo k900,this phone have a function is forbid some application use camera or other permission.so I need to know how to judge whether app can use camera.

Upvotes: 0

Views: 683

Answers (2)

Carl
Carl

Reputation: 250

I'm very glad to finally solved the problem.
first I judge whether the system camera can be use, if not give user a prompt ,use this method

 private Camera getCameraInstance() {
        try {
            camera = Camera.open(); // attempt to get a Camera instance
        } catch (Exception e) {
            LogUtil.log(LogUtil.TAG_ERROR, e.toString());
            DialogUtil.showToast(getString(R.string.camera_can_not_use));
            finish();
        }
        return camera; // returns null if camera is unavailable
    }

and then, before call system camera,call this method to check. if this method return is not null.remind release the camera.

private void doTakePhoto() {
        if (getCameraInstance() != null) {
            camera.release();
            Intent intent = new Intent();
            intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivityForResult(intent, CAMERA_REQUEST_CODE);
        }
    }

now when system forbid your app use camera you can give user a friendly prompt

Upvotes: 2

Shereef Marzouk
Shereef Marzouk

Reputation: 3392

try {
    // Your camera code
} catch (Exception e) {
    // Tell the user nicely
}

Updated answer:

public boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List resolveInfo =
            packageManager.queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
   if (resolveInfo.size() > 0) {
     return true;
    }
   return false;
}

if (isIntentAvailable(this, MediaStore.ACTION_IMAGE_CAPTURE)){  
    Intent i = new Intent();
    i.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile));    
    startActivityForResult(intent, CAMERA_REQUEST_CODE); 
} else {
// tell the user nicely or create a chooser intent.createChooser
}

Try this and let me know

Upvotes: 0

Related Questions