1

I am trying to save public files on the external storage. I am following the example from Android Developers page: https://developer.android.com/training/data-storage/files#PublicFiles

First I tried to create a directory "mydocuments" in the public directory DIRECTORY_DOCUMENTS. The code is as simple as

TextView tv= findViewById(R.id.myTextview);
    // Get the directory for the user's public pictures directory.
    File documents= Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_DOCUMENTS);
    tv.append("\n documents directory=" + documents);
    File file = new File(documents, "mydocuments");
    tv.append("\n\nDirectory to be created=" + file);
    if (file.exists())
        tv.append("\n\nFile already exists:" + file.getAbsolutePath());
    else{
        if (!file.mkdirs())
            tv.append("\n\nDirectory not created:" + file.getAbsolutePath());

I added permissions in the manifest file

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

However when I run this in a device (be virtual or physical) the result is always the same:

documents directory=/storage/emulated/0/Documents

Directory to be created=/storage/emulated/0/Documents/mydocuments

Directory not created:/storage/emulated/0/Download/mydocuments

How to make the Documents directory writeable?

Thank you in advance.

amarougr
  • 11
  • 1
  • 4
  • `documents=/storage/emulated/0/Documents Directory not created` That is a confusing way to post your problem as the directory that not got created is `/storage/emulated/0/Documents/mydocuments` – greenapps Aug 19 '18 at 09:19
  • `tv.append("Directory not created");` Change to `tv.append("Directory not created: " + file.getAbsolutePath());` – greenapps Aug 19 '18 at 09:20
  • Further: You copied this code from official google docs. Its pretty bad code as you will get a message that the directory is not created when the directory already exists. You should only call mkdir()/mkdirs() if the directory does not exist yet. So check before use. – greenapps Aug 19 '18 at 09:25
  • thank you I edited my code above to something more clear including checking if the directory exists. – amarougr Aug 19 '18 at 10:42

2 Answers2

0

You are #1278 with this problem this year.

For Android 6+ you have to add code to ask the user to confirm the permissions you requested in manifest file.

Google for runtime permissions.

You could also go to the settings of your app and toggle the storage permission to on.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Thanks to @greenapps suggestion I solved my problem. This is the simplest solution I found to write in the Documents directory on the SD card, as explained in

https://developer.android.com/training/permissions/requesting

I have copied some of that here:

1.- First ask for user permissions somewhere at the begining of the code:

  ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            MY_PERMISSIONS);

where I previously defined the constant

final int MY_PERMISSIONS=1;

2.- Then implement my code above to write on the SD card inside the method

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // task you need to do.
                writeSD();  // this is the code to write on the SD card shown above

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                tv.append("\n\n No permission given");
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

This solves the issue for me. The first time it is executed, a dialog opens asking for the permissions. After that It is allowed to read the contents of the sdcard and to write in the public directories DOCUMENTS, PICTURES, DOWNLOADS, etc.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
amarougr
  • 11
  • 1
  • 4