Home > Blockchain >  Android cannot create directory
Android cannot create directory

Time:06-05

I cannot create directory, I have all the permissions and this in my Manifest:

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

In MainActivity onCreate, checks permission, if it has it should create a directory but it always returns a false:

if (!checkPermission()) requestPermission();
    else {
        File folder = new File(Environment.getExternalStorageDirectory()  
                File.separator   "receipts");
        if (!folder.exists()) {
            boolean bool = folder.mkdirs();
            System.out.println(bool);
        }
    }

Any clue or hint to why? Thanks

CodePudding user response:

Unfortunately, with the security updates brought by Android 11 and up, as @CommonsWare said, you simply can't write directories on external storage (sdcard).

Straight from the docs:

Access to directories

You can no longer use the ACTION_OPEN_DOCUMENT_TREE intent action to request access to the following directories:

The root directory of the internal storage volume. The root directory of each SD card volume that the device manufacturer considers to be reliable, regardless of whether the card is emulated or removable. A reliable volume is one that an app can successfully access most of the time. The Download directory.

Additionally from the same place:

App-specific directory on external storage Starting in Android 11, apps cannot create their own app-specific directory on external storage. To access the directory that the system provides for your app, call getExternalFilesDirs().

Your app has a system generated directory to store any information. This makes sense, of course, because at any given time the user could remove/format the sd card inside the device, and your app's data would be entirely lost.

From more docs:

You would use this to write:

//Write to a file
String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = context.openFileOutput(filename, Context.MODE_PRIVATE)) {
    fos.write(fileContents.toByteArray());
}

And to read a file:

//To read from the file
FileInputStream fis = context.openFileInput(filename

);
InputStreamReader inputStreamReader =
        new InputStreamReader(fis, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
try (BufferedReader reader = new BufferedReader(inputStreamReader)) {
    String line = reader.readLine();
    while (line != null) {
        stringBuilder.append(line).append('\n');
        line = reader.readLine();
    }
} catch (IOException e) {
    // Error occurred when opening raw file for reading.
} finally {
    String contents = stringBuilder.toString();
}

These are both within your app's "sandbox" folder. Because of this, you do not need to declare permissions.

CodePudding user response:

File folder = new File(Environment.getExternalStorageDirectory() File.separator "receipts");

Change to:

 File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "receipts");
  • Related