android javaでユーザからカメラ、写真フォルダへの許可を取る方法

#android javaでユーザからカメラ、写真フォルダへの許可を取る方法
    
    import android.Manifest;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.DialogInterface;
    import android.content.pm.PackageManager;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.PermissionChecker;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends Activity {
    
        private static final int REQUEST_CODE_CAMERA_PERMISSION = 1000;
        private static final int REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION = 2000;
    
        String title = "権限チェック";
        String message = "この機能を使用する際には権限を許可する必要があります。";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button requestButtonCamera = (Button) findViewById(R.id.requestButton1);
            requestButtonCamera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    // カメラの権限状態取得
                    int permissionCamera = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
    
                    // カメラの権限確認
                    if (permissionCamera != PackageManager.PERMISSION_GRANTED) {
                        requestPermission(REQUEST_CODE_CAMERA_PERMISSION);
                    }
    
                   // TODO : アクセス後の処理
    
                }
            });
    
            Button requestButtonWriteExternalStoragePermission = (Button) findViewById(R.id.requestButton2);
            requestButtonWriteExternalStoragePermission.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    // カメラロールの権限状態取得
                    int permissionWriteExternalStorage = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    
                    // カメラロールの権限確認
                    if (permissionWriteExternalStorage != PackageManager.PERMISSION_GRANTED) {
                        requestPermission(REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION);
                    }
    
                    // TODO : アクセス後の処理
    
                }
            });
    
        }
    
        // Permission handling for Android 6.0
        private void requestPermission(int requestCode) {
    
            // 権限チェックした結果、持っていない場合はダイアログを出す
            switch (requestCode) {
    
                case REQUEST_CODE_CAMERA_PERMISSION:
                        alert(requestCode,new String[]{Manifest.permission.CAMERA},title, message);
                    break;
    
                case REQUEST_CODE_WRITE_EXTERNAL_STORAGE_PERMISSION:
                        alert(requestCode,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},title, message);
                    break;
            }
        }
    
        private void alert(final int requestCode,final String[] manifestPermission, String title,String message){
    
            DialogInterface.OnClickListener dialog = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCompat.requestPermissions(MainActivity.this,
                            manifestPermission,
                            requestCode);
                }
            };
    
            new AlertDialog.Builder(this)
                    .setTitle(title).setMessage(message)
                    .setPositiveButton(android.R.string.ok,dialog).create().show();
        }
    }