Home >Java >javaTutorial >Why Can't I Create a Directory in Android 10, Even with Necessary Permissions?

Why Can't I Create a Directory in Android 10, Even with Necessary Permissions?

Susan Sarandon
Susan SarandonOriginal
2024-12-14 19:04:13443browse

Why Can't I Create a Directory in Android 10, Even with Necessary Permissions?

I'm unable to create directory in Android 10. It's working on devices till Android Oreo . I tried two ways for creating folders.

Using
    File.mkdir():
  • Here, the variable success is always
false
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");
if (!f.isFile()) {
    if (!(f.isDirectory())) {
        success =  f.mkdir();
    }
}
which means the directory isn't created.

Using
    Files.createDirectory():
  • which causes this exception:
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");
if (!f.isFile()) {
    if (!(f.isDirectory())) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            try {
                Files.createDirectory(Paths.get(f.getAbsolutePath()));
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), R.string.unable_to_download, Toast.LENGTH_LONG).show();
            }
        } else {
            f.mkdir();
        }
    }
}

I've implemented the run-time permissions and

pzy64.pastebinpro W/System.err: java.nio.file.AccessDeniedException: /storage/emulated/0/Pastebin
pzy64.pastebinpro W/System.err:     at sun.nio.fs.UnixFileSystemProvider.createDirectory(UnixFileSystemProvider.java:391)
pzy64.pastebinpro W/System.err:     at java.nio.file.Files.createDirectory(Files.java:674)
`

`

are all set.

Answer:

As first revealed in March 2019, you can no longer access any external storage or removable storage by default on Android 10 Location. This includes Environment.getExternalStorageDirectory() and other methods on Environment (for example, getExternalStoragePublicDirectory()).

For Android 10 and 11, you can add android:requestLegacyExternalStorage="true" to the element in the manifest. This will let you choose traditional storage mode, and your existing external storage code will also work.

Otherwise, your options are:

Use methods on the Context (such as getExternalFilesDir()) to access directories on external storage that your app can write to. You don't need any permissions to use these directories on Android 4.4. However, when your app is uninstalled, the data stored here will be deleted.

    Use storage access frameworks (such as ACTION_OPEN_DOCUMENT and ACTION_CREATE_DOCUMENT).
  • If your content is media, you can use MediaStore to place the media in standard media locations.

The above is the detailed content of Why Can't I Create a Directory in Android 10, Even with Necessary Permissions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn