2016-11-18 84 views
0

情況:當相機(imageview)被點擊時,會提示多重權限。Android多重權限錯誤

  1. 如果用戶在第一個權限和第二個權限上選擇了DENY,那麼它正在工作。
  2. 如果用戶在第一個權限和第二個權限上選擇ALLOW,則其工作。
  3. 如果用戶在第一個權限中選擇了DENY並且在第二個權限上選擇了ALLOW,則它正在工作。
  4. 如果用戶在第一個權限上選擇了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 
+0

在交換機的情況下grantResults.length> 0真實的,但ü沒有給予相機許可,所以它的拋出異常 – Raghavendra

+0

在調用OpenCamera()時檢查攝像頭權限() – Raghavendra

回答

1

的問題是,你不能正確處理onRequestPermissionResult。 在此行中

if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
       OpenCamera();} 

你是隻檢查您所要求的女巫在這種情況下Manifest.permission.READ_EXTERNAL_STORAGE第一許可(位置== 0),你觸發你的相機功能巫婆需要允許在位置== 2

解決方案

if (grantResults.length > 0 && grantResults[2] == PackageManager.PERMISSION_GRANTED) { 
       OpenCamera();} 

OR

if (grantResults.length > 0 && (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED)) { 
       OpenCamera();} 

//您也可以使用循環

+0

我明白了。如果只有1個權限,怎麼樣?它還在爲超過1個陣列工作嗎? – Nizzam

+0

找到解決方案!感謝你們。 – Nizzam

0

這是因爲您只檢查第一個許可結果,而不檢查第二個:grantResults[0] == PackageManager.PERMISSION_GRANTED。你需要檢查 grantResults[1] == PackageManager.PERMISSION_GRANTED以及

0

如果您的要求是,如果給定的,那麼所有的權限,只有它有工作,你可以嘗試這樣的

@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) { 
        boolean permGiven = true; 
        for(int i=0; i<grantResults.length; i++){ 
         if(grantResults[i] != PackageManager.PERMISSION_GRANTED) 
          permGiven = false; 
        } 
        if(permGiven) 
         OpenCamera(); 
        else 
         Toast.makeText(MainActivity.this, "Go to settings and enable permissions", Toast.LENGTH_LONG).show(); 
       } else { 
        Toast.makeText(MainActivity.this, "Go to settings and enable permissions", Toast.LENGTH_LONG).show(); 
       } 
       break; 


     } 
    }