Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hosseiniSeyRo/c7965e4ea4a7b65774619ce32417af96 to your computer and use it in GitHub Desktop.
Save hosseiniSeyRo/c7965e4ea4a7b65774619ce32417af96 to your computer and use it in GitHub Desktop.
permission
const val REQUEST_EXTERNAL_STORAGE = 1001
fun isStoragePermissionGranted(context: Context, activity: Activity): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(
context, Manifest.permission.READ_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
) {
// Permission is granted
true
} else {
// Permission is revoked
ActivityCompat.requestPermissions(
activity,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
REQUEST_EXTERNAL_STORAGE
)
false
}
} else { //permission is automatically granted on sdk<23 upon installation
// Permission is granted
true
}
}
override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
REQUEST_EXTERNAL_STORAGE -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
imagePicker.start()
}
}
else -> {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment