How to ask runtime permissions for Camera in Android , Runtime storage permissions

后端 未结 6 996
迷失自我
迷失自我 2020-12-01 16:47

I am doing an application to scan barcodes on a button click and it was working fine up to Lollipop versions. When I came to Marshmallow it stopped working. This is the erro

6条回答
  •  無奈伤痛
    2020-12-01 16:58

    RxPermission is best library for asking permissions from user.

    For camera permission the workaround is like this.

    1) First add these permissions (or one you need) in your manifest.xml.

    
    

    2) Then ask run time permission from user in your activity.

    RxPermissions rxPermissions = new RxPermissions(this);
    rxPermissions
    .request(Manifest.permission.CAMERA) // ask single or multiple permission once
    .subscribe(granted -> {
        if (granted) {
           // All requested permissions are granted
        } else {
           // At least one permission is denied
        }
    });
    

    add this library in your build.gradle

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    dependencies {
        implementation 'com.github.tbruyelle:rxpermissions:0.10.1'
        implementation 'com.jakewharton.rxbinding2:rxbinding:2.1.1'
    }
    

    Isn't this easy?

提交回复
热议问题