安卓有一个问题:我的安卓应用程序需要使用系统摄像头,function.it可以很好地工作,但是当它在lenovo k900上运行时,这款手机有一个功能是禁止某些应用程序使用摄像头或其他permission.so,我需要知道如何判断应用程序是否可以使用摄像头。
发布于 2014-06-11 02:58:17
我很高兴终于解决了这个问题。
首先,我判断是否可以使用系统摄像机,如果不给用户一个提示,使用此方法。
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
}然后,在调用系统摄像机之前,调用此方法进行检查。如果此方法返回的不是null.remind,则释放摄像机。
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);
}
}现在,当系统禁止应用程序使用相机时,您可以给用户一个友好的提示。
发布于 2014-06-09 02:43:41
try {
// Your camera code
} catch (Exception e) {
// Tell the user nicely
}最新答复:
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
}试试这个,让我知道
https://stackoverflow.com/questions/24112991
复制相似问题