Home >Java >javaTutorial >Why Does Creating Directories Fail in Android 10 and How Can I Fix It?

Why Does Creating Directories Fail in Android 10 and How Can I Fix It?

Barbara Streisand
Barbara StreisandOriginal
2024-12-06 01:52:13406browse

Why Does Creating Directories Fail in Android 10 and How Can I Fix It?

Unable to Create Directory in Android 10

Problem:

Creating directories in Android 10 using either File.mkdir() or Files.createDirectory() fails, resulting in false success status and an AccessDeniedException.

Error:

java.nio.file.AccessDeniedException: /storage/emulated/0/Pastebin

Cause:

Android 10 restricts access to external storage locations by default.

Solution:

Use Legacy Storage Access (Android 10 and 11 Only)

Add the following to the tag in the manifest:

android:requestLegacyExternalStorage="true"

This reverts to the legacy storage model, allowing existing external storage code to function.

Alternative Options

  • Context Methods: Use methods on Context, such as getExternalFilesDir(), to access directories on external storage.
  • Storage Access Framework: Use the Storage Access Framework to access documents and create folders.
  • MediaStore: Use MediaStore for managing media content.

The above is the detailed content of Why Does Creating Directories Fail in Android 10 and How Can I Fix It?. 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