Android Studio

android permission 파일 저장

i-moo 2017. 4. 7. 16:57
반응형

참고 사이트

http://thdev.net/634 (taehwan님의 블로그)


/**
* Permission check.
*/
private void checkPermission() {
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED
|| checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Explain to the user why we need to write the permission.
Toast.makeText(this, "Read/Write external storage", Toast.LENGTH_SHORT).show();
}

requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
1000);

// MY_PERMISSION_REQUEST_STORAGE is an
// app-defined int constant

} else {

// 다음 부분은 항상 허용일 경우에 해당이 됩니다.

}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1000:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {

// 허락일 경우에 해당이 됩니다.



// permission was granted, yay! do the
// calendar task you need to do.

} else {

Log.d("PERMISSION", "Permission always deny");

// permission denied, boo! Disable the
// functionality that depends on this permission.
}
break;
}
}


반응형