boolean isGrantStorage = grantExternalStoragePermission();


if(isGrantStorage){

// 일반처리.

}




private boolean grantExternalStoragePermission() {

    if (Build.VERSION.SDK_INT >= 23) {


        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

            Log.v(TAG,"Permission is granted");

            return true;

        }else{

            Log.v(TAG,"Permission is revoked");

            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);


            return false;

        }

    }else{

        Toast.makeText(this, "External Storage Permission is Grant", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "External Storage Permission is Grant ");

        return true;

    }


}



@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if (Build.VERSION.SDK_INT >= 23) {

    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){

        Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);

        //resume tasks needing this permission

    }

    }

}




if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

    Log.v(TAG,"Permission is granted");

    return true;

}


ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);




◇ 해당권한의 기능사용 시 checkSelfPermission 을 사용하여 사용자가 권한을 승인해야만 API의 사용이 가능.

또한, Manifest에서 uses-permission 으로 선언된 기능에 대해서만 동의진행이 가능하다


import android.Manifest;

final int PERMISSION = 1;


if ( Build.VERSION.SDK_INT >= 23 

&&ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED 

|| ContextCompat.checkSelfPermission( this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)

|| ContextCompat.checkSelfPermission( this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)

|| ContextCompat.checkSelfPermission( this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)

{


// 전화, 위치수신, 카메라에 대한 권한요청, String배열로 복수개의 요청이 가능함


ActivityCompat.requestPermissions(this, new String[]{ {

Manifest.permission.CALL_PHONE,

Manifest.permission.ACCESS_COARSE_LOCATION,

Manifest.permission.ACCESS_FINE_LOCATION,

Manifest.permission.CAMERA}, PERMISSION);


}


@Override

public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {

switch (requestCode) {

case PERMISSION:


// grantResult는 requestPermissions에서 요청된 String[]순서로 들어옴. 0~N개로 결과를 탐색하면 된다

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

// 권한 허가

// 해당 권한을 사용해서 작업을 진행할 수 있음

} else {

// 권한 거부

// 사용자가 해당권한을 거부했을때 해주어야 할 동작을 수행

}

return;

}

}




//checking if you have the permission

if (ContextCompat.checkSelfPermission(thisActivity,

                Manifest.permission.READ_CONTACTS)

        != PackageManager.PERMISSION_GRANTED) {

   // you don't have permission so here you will request the user for permission


  if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,

        Manifest.permission.READ_CONTACTS)) {


        // Show an explanation to the user *asynchronously* -- don't block

        // this thread waiting for the user's response! After the user

        // sees the explanation, try again to request the permission.


    } else {


        // No explanation needed, we can request the permission.


        ActivityCompat.requestPermissions(thisActivity,

                new String[]{Manifest.permission.READ_CONTACTS},

                MY_PERMISSIONS_REQUEST_READ_CONTACTS);


        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an

        // app-defined int constant. The callback method gets the

        // result of the request.

    }


}