情況:當相機(imageview)被點擊時,會提示多重權限。Android多重權限錯誤
- 如果用戶在第一個權限和第二個權限上選擇了DENY,那麼它正在工作。
- 如果用戶在第一個權限和第二個權限上選擇ALLOW,則其工作。
- 如果用戶在第一個權限中選擇了DENY並且在第二個權限上選擇了ALLOW,則它正在工作。
- 如果用戶在第一個權限上選擇了ALLOW,在第二個權限上選擇了DENY,則會出現錯誤。
這裏是代碼(擴展片段):
private static String[] PERMISSIONS_CAMERA = {Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA};
img_1 = (ImageView) view.findViewById(R.id.img_1);
img_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startCamera();
}
});
private void StartCamera() {
requestPermissions(PERMISSIONS_CAMERA, REQUEST_CAMERA);
}
private void OpenCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
this.bitmap = null;
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
// For camera
case REQUEST_CAMERA:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
OpenCamera();
} else {
Toast.makeText(getActivity(), "Go to settings and enable permissions", Toast.LENGTH_LONG).show();
}
break;
}
}
錯誤日誌:
java.lang.RuntimeException: Failure delivering result
ResultInfo{[email protected]:requestPermissions:, request=131073,
result=-1, data=Intent {
act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to
activity {app.swb.mont/app.swb.mont.ui.activity.MainActivity}:
java.lang.SecurityException: Permission Denial: starting Intent {
act=android.media.action.IMAGE_CAPTURE
cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{b2f7ceb
28260:app.swb.mont/u0a392} (pid=28260, uid=10392) with revoked
permission android.permission.CAMERA at android.app.ActivityThread.deliverResults(ActivityThread.java:4925)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4968)
at android.app.ActivityThread.access$1600(ActivityThread.java:222)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1849)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7229)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE
cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{b2f7ceb
28260:app.swb.mont/u0a392} (pid=28260, uid=10392) with revoked
permission android.permission.CAMERA
在交換機的情況下grantResults.length> 0真實的,但ü沒有給予相機許可,所以它的拋出異常 – Raghavendra
在調用OpenCamera()時檢查攝像頭權限() – Raghavendra