早上好,
我使用以下代码拍摄照片并在Imageview上显示。
private static final int CAMERA_REQUEST = 1888;
public void OpenCamera(){
AddImage.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
);
}
// for getting displaying the picture on the image view
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// to remove the main background and draw the picture
AddImage.setBackgroundResource(0);
AddImage.setImageBitmap(photo);
//Barcode();
}
}
一切正常,直到导入'zxing'库为生成的图像生成QR码。但是,我在“清单”中检查了相机权限并且工作正常,许可的代码是:
<uses-permission android:name="android.permission.CAMERA"> </uses-permission>
检查它的方法是(它返回true):
public boolean hasPermissionInManifest(Context context, String permissionName) {
final String packageName = context.getPackageName();
try {
final PackageInfo packageInfo = context.getPackageManager()
.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
final String[] declaredPermisisons = packageInfo.requestedPermissions;
if (declaredPermisisons != null && declaredPermisisons.length > 0) {
for (String p : declaredPermisisons) {
if (p.equals(permissionName)) {
return true;
}
}
}
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
现在,当“startActivityForResult(cameraIntent,CAMERA_REQUEST);”时出现以下错误:被称为:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: iq.wissam.CoiApp, PID: 3853
java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera } from ProcessRecord{88f1abb 3853:iq.nazaha.wissam.CoiApp/u0a85} (pid=3853, uid=10085) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1683)
at android.os.Parcel.readException(Parcel.java:1636)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3071)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at iq.nazaha.wissam.CoiApp.AddNewItemPage$1.onClick(AddNewItemPage.java:91)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
提前致谢。