There is the following code:

public boolean write() { String jsonString = ""; StringBuilder sb = new StringBuilder(); ArrayList<SData> list = (ArrayList<SData>) getAll(); for (SData s : list) { sb.append(s.toRawString() + "\n"); } jsonString = sb.toString(); boolean result = true; try { File file = new File(Environment.getExternalStorageDirectory().getPath() + "/sData.txt"); FileWriter fileWriter = new FileWriter(file); BufferedWriter out = new BufferedWriter(fileWriter); out.write(jsonString); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); result = false; } catch (IOException e) { e.printStackTrace(); result = false; } return result; } 

On versions prior to api, level 23 works fine, 23 needs permissions, you can request them like this:

  private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; /** * Checks if the app has permission to write to device storage * * If the app does not has permission then the user will be prompted to grant permissions * * @param */ public void verifyStoragePermissions() { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(ListActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( ListActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } 

but they are requested at the time of execution.

The question is whether it is possible to request these permissions at the time of installation and if not, what other ways are there to read / write data to the file without waiting for permission to be obtained?

  • one
    There are no such methods, for this purpose, these permissions were entered in 6k so that the application would not write anything anywhere until the user does not approve the rights - xkor

1 answer 1

There are no such methods, since version 6.0, Dangerous permissions were introduced to increase security, which are called up at the time of use