Reputation: 448
I am trying to get 2 permissions at once but Iam unable to do so.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(context, Array<String>(2) {
Manifest.permission.READ_CONTACTS;
Manifest.permission.WRITE_EXTERNAL_STORAGE},
1);
}
}
It only asks for WRITE_EXTERNAL_STORAGE
permission and not READ_CONTACTS
. Actually, If I write WRITE_EXTERNAL_STORAGE
one first and READ_CONTACTS
after that then it will only ask for contact and not WRITE_EXTERNAL_STORAGE
.
Upvotes: 4
Views: 2208
Reputation: 29924
Array(size: Int, init: (Int) -> T)
is the constructor for the Array
class which takes the number of elements it should contain, and an init
function which maps the index to an actual array element.
Here is what your code does, written in a more verbose way
val a = Array<String>(size = 2, init = { index: Int ->
Manifest.permission.READ_CONTACTS; // nothing
Manifest.permission.WRITE_EXTERNAL_STORAGE // returned by the lambda for each index
})
Result:
[Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE]
So, you would be better of using the arrayOf
function in this case, since you don't want to genereate your array elements but rather specify them explicitely.
Upvotes: 1
Reputation: 424
Use this code for multiple permission in kotlin
ActivityCompat.requestPermissions(context as Activity,
arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE),
PERMISSION_CAMER)
Upvotes: 2
Reputation: 25623
Array<String>(2) {
Manifest.permission.READ_CONTACTS ;
Manifest.permission.WRITE_EXTERNAL_STORAGE
}
This initializer always returns WRITE_EXTERNAL_STORAGE
, the first line does not do anything. You should probably be using the arrayOf
function to construct the array instead.
Upvotes: 1