Issue with permissions on Android Marshmallow [duplicated]

0

I am developing an application that allows you to upload a profile picture. For this I access the gallery through an intent.

The problem comes with the read permissions, I solved them, in part, including in the manifest the following:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

On a smartphone with Android Lollipop the problem is solved, but when running the application on a smartphone with Android Marshmallow the following error still appears:

java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/file/38720 from pid=20457, uid=10195 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

From what I have read and little that I have understood nothing can be done from the manifest, if you do not have to add code in the activity itself, this is where I get totally lost.

Thank you.

    
asked by Daniel Plata 14.03.2017 в 20:46
source

1 answer

0

From Marshmallow you have to request the privileges in Runtime to the user. I give you an example (in this case for phone and SMS):

static final String[] PHONE_PERMISSIONS = new String[]{Manifest.permission.CALL_PHONE,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.PROCESS_OUTGOING_CALLS};
static final String[] SMS_PERMISSIONS = new String[]{Manifest.permission.SEND_SMS,
        Manifest.permission.READ_SMS,
        Manifest.permission.RECEIVE_SMS};
static final int PHONE_PERMISSIONS_REQUEST=0;
static final int SMS_PERMISSIONS_REQUEST=1;


public void onCreate(Bundle bundle){
    // más código de onCreate

    if (!hasPhonePermissions()){
        ActivityCompat.requestPermissions(this,PHONE_PERMISSIONS,PHONE_PERMISSIONS_REQUEST);
    } else if (!hasSmsPermissions()){
        ActivityCompat.requestPermissions(this,SMS_PERMISSIONS,SMS_PERMISSIONS_REQUEST);
    }

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case PHONE_PERMISSIONS_REQUEST:
            // filter logic
            if (!hasSmsPermissions()) ActivityCompat.requestPermissions(this,SMS_PERMISSIONS,SMS_PERMISSIONS_REQUEST);
            for (int i=0;i<permissions.length;i++){
                Log.d("Permission: ",String.format("Phone %d) granted %b", i+1,grantResults[i]==PackageManager.PERMISSION_GRANTED ));
            }
            break;
        case SMS_PERMISSIONS_REQUEST:
            for (int i=0;i<permissions.length;i++){
                // reaccionar a permiso o rechazo
                Log.d("Permission: ",String.format("Sms %d) granted %b", i+1,grantResults[i]==PackageManager.PERMISSION_GRANTED ));
            }
            break;
    }
}

private boolean hasPhonePermissions(){
    boolean hp=true;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
            == PackageManager.PERMISSION_GRANTED;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE)
            == PackageManager.PERMISSION_GRANTED;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.PROCESS_OUTGOING_CALLS)
            == PackageManager.PERMISSION_GRANTED;
    return hp;
}

private boolean hasSmsPermissions(){
    boolean hp=true;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
            == PackageManager.PERMISSION_GRANTED;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
            == PackageManager.PERMISSION_GRANTED;
    hp = hp && ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS)
            == PackageManager.PERMISSION_GRANTED;
    return hp;
}

In your case you have to process Manifest.permission.READ_EXTERNAL_STORAGE . Concrete:

boolean priv = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
        == PackageManager.PERMISSION_GRANTED;

if (!priv){
    String[] privilegios = { Manifest.permission.READ_EXTERNAL_STORAGE };
    ActivityCompat.requestPermissions(this, privilegios,0);
}

@Override
public void onRequestPermissionsResult(int rc, @NonNull String[] privilegios, @NonNull int[]resultados){
     // evaluar si se obtuvo el privilegio
}
    
answered by 14.03.2017 / 21:58
source