Created
April 6, 2021 07:54
-
-
Save hosseiniSeyRo/c7965e4ea4a7b65774619ce32417af96 to your computer and use it in GitHub Desktop.
permission
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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