【发布时间】:2014-06-09 02:39:51
【问题描述】:
android有一个问题:我的android应用程序需要使用系统摄像头功能。它可以很好地工作,但是当它在lenovo k900上运行时,这个手机有一个功能是禁止某些应用程序使用摄像头或其他权限。所以我需要知道如何判断应用程序是否可以使用相机。
【问题讨论】:
标签: android permissions camera system
android有一个问题:我的android应用程序需要使用系统摄像头功能。它可以很好地工作,但是当它在lenovo k900上运行时,这个手机有一个功能是禁止某些应用程序使用摄像头或其他权限。所以我需要知道如何判断应用程序是否可以使用相机。
【问题讨论】:
标签: android permissions camera system
我很高兴终于解决了这个问题。
首先判断系统摄像头是否可以使用,如果不给用户提示,使用此方法
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。提醒释放相机。
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);
}
}
现在当系统禁止您的应用使用相机时,您可以给用户一个友好的提示
【讨论】:
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
}
试试这个,让我知道
【讨论】:
onActivityResult()没有被调用。