Android呼叫相簿、相機(相容6.0、7.0、8.0)所需新增的許可權

Tms_M發表於2018-10-24

在AndroidManifest.xml中新增:

    <!--讀寫記憶體塊許可權-->
  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!--呼叫相機許可權-->
  <uses-permission android:name="android.permission.CAMERA" />

動態許可權新增:
在onCreate中新增

													//調取系統照相機
 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &&
                ContextCompat.checkSelfPermission(this, 
                Manifest.permission.WRITE_EXTERNAL_STORAGE) 
                !=
                 PackageManager.PERMISSION_GRANTED
                ) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        } else {
            wxat = 200;
        }

方法:

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(this, "授權成功", Toast.LENGTH_SHORT).show();
                wxat = 200;
            } else {
                Toast.makeText(this, "授權失敗", Toast.LENGTH_SHORT).show();
            }

        }
    }

或者使用easypermissions框架 (https://github.com/googlesamples/easypermissions)

1、首先也是在AndroidManifest.xml中新增:

<!--讀寫記憶體塊許可權-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!--呼叫相機許可權-->
<uses-permission android:name="android.permission.CAMERA" />

2、在主頁面中:

2.1、初始化集合裝載所需網路許可權

private String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};

2.2、檢查許可權申請許可權

//獲取許可權
    private void getPermission() {
        if (EasyPermissions.hasPermissions(this, permissions)) {
            //已經開啟許可權
            Toast.makeText(this, "已經申請相關許可權", Toast.LENGTH_SHORT).show();
        } else {
            //沒有開啟相關許可權、申請許可權
            EasyPermissions.requestPermissions(this, "需要獲取您的相簿、照相使用許可權", 1, permissions);
        }
    }

2.3、實現介面

在所運用許可權申請的介面實現EasyPermissions.PermissionCallbacks介面

2.4、處理介面回撥的資料

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        //框架要求必須這麼寫
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    
    //成功開啟許可權
    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        
        Toast.makeText(this, "相關許可權獲取成功", Toast.LENGTH_SHORT).show();
    }
    //使用者未同意許可權
    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Toast.makeText(this, "請同意相關許可權,否則功能無法使用", Toast.LENGTH_SHORT).show();
    }

相關文章