简体   繁体   中英

How to create a public directory on the external storage?

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM