Fail to request permission api 26 android 8

1

I'm quite new and I do not know how to assign permissions in api 26, the app works and I get the box to approve the permissions but also on top of the message that the app has failed, the function in which it falls is:

 requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE},

When requesting permission, I tried to put a try catch but I did not catch the error.

In api 24 android 7 if everything works well

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btnRunApp = (Button) findViewById(R.id.btnRunApp);
            btnSolicitarPermisos = (Button) findViewById(R.id.btnSolicitarPermisos);

            btnSolicitarPermisos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(validaPermisos()){

                        Toast.makeText(getApplicationContext(),"Tiene permiso de escritura", Toast.LENGTH_LONG).show();
                        btnRunApp.setEnabled(true);
                    }else {
                        Toast.makeText(getApplicationContext(),"No Tiene permiso de escritura", Toast.LENGTH_LONG).show();
                    }
                }
            });

        }

        private boolean validaPermisos() {

            if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
                return true;
            }

            if((checkSelfPermission(READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)&&
                    (checkSelfPermission(WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)){
                return true;
            }

            if((shouldShowRequestPermissionRationale(READ_EXTERNAL_STORAGE)) ||
                    (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE))){
            }else{

//falla aqui al ejecutar esta función
                requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE},100);
            }

            return false;
        }

        private void cargarDialogoRecomendacion() {
            AlertDialog.Builder dialogo=new AlertDialog.Builder(MainActivity.this);
            dialogo.setTitle("Permisos Desactivados");
            dialogo.setMessage("Debe aceptar los permisos para el correcto funcionamiento de la App");

            dialogo.setPositiveButton("Aceptar", new DialogInterface.OnClickListener() {


                @RequiresApi(api = Build.VERSION_CODES.M)
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE},100);
                }
            });
            dialogo.show();
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);

            if(requestCode==100){
                if(grantResults.length==2 && grantResults[0]==PackageManager.PERMISSION_GRANTED
                        && grantResults[1]==PackageManager.PERMISSION_GRANTED){
                    btnRunApp.setEnabled(true);
                }
            }
        }



    }
    
asked by jesususo 01.05.2018 в 09:57
source

1 answer

0

You must use the full notation to access the class constant Manifest , this is NOT incorrect :

requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE},100);
                }

The right thing should be:

requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},100);
                }

The same for the method that validates permissions:

      private boolean validaPermisos() {

            if(Build.VERSION.SDK_INT<Build.VERSION_CODES.M){
                return true;
            }

            if((checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)&&
                    (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)==PackageManager.PERMISSION_GRANTED)){
                return true;
            }

            if((shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) ||
                    (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE))){
            }else{

//falla aqui al ejecutar esta función
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},100);
            }

            return false;
        }
    
answered by 03.08.2018 в 00:13