I make a program where you want to save / read the file to external memory. While working on the emulator and Android 5 everything was fine. Installed on Android 6 and write / read does not work. No errors and crashes occur. What is noteworthy when you start and update the application is not requesting permissions, although in the manifest they are.

My application is poorly readable, so I offer an example on which the error is reproduced: example application

Also add that the device with Android 6 - Zuk Z1. But I doubt that it is in him.

  • one
    Do you request permission to access external memory before attempting to write / read? In Android 6 and above, they must now be requested by code. Manifesto is not enough) - xkor
  • OU! And in what part of the application? - Alexander Tymchuk
  • 2
    In any way, you need to read or write - check and you have right now, if not, then you ask and wait if the user will give them to you, if he does not let him show him a sad smiley and report that he cannot work in such conditions) Details here - xkor 7:44 pm

1 answer 1

The video helped a lot , as I could not find examples.

In short: create a variable

private final int MY_PERMISSIONS_REQUEST_CODE = 1; 

create an overloaded method

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode != MY_PERMISSIONS_REQUEST_CODE) { return; } boolean isGranted = true; for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { isGranted = false; break; } } if (isGranted) { Toast.makeText(MainActivity.this, "Разрешения получены", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "В разрешениях отказано", Toast.LENGTH_LONG).show(); } } 

we create function of check of permissions

 private boolean checkPermissions() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { return false; } return true; } 

permission setting function

 private void setPermissions() { ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_CODE); } 

and in the code we call check

  if (checkPermissions()) { Toast.makeText(MainActivity.this, "Разрешения уже получены", Toast.LENGTH_SHORT).show(); } else { setPermissions(); } 
  • Can this be taken out in a separate class and called when necessary? - Vyacheslav