Can't create directory in Android 10
As was first disclosed back in March 2019, you no longer have access by default to arbitrary locations on external storage or removable storage on Android 10+. This includes Environment.getExternalStorageDirectory()
and other methods on Environment
(e.g., getExternalStoragePublicDirectory()
.
For Android 10 and 11, you can add android:requestLegacyExternalStorage="true"
to your <application>
element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.
Otherwise, your choices are:
Use methods on
Context
, such asgetExternalFilesDir()
, to get at directories on external storage into which your app can write. You do not need any permissions to use those directories on Android 4.4+. However, the data that you store there gets removed when your app is uninstalled.Use the Storage Access Framework, such as
ACTION_OPEN_DOCUMENT
andACTION_CREATE_DOCUMENT
.If your content is media, you can use
MediaStore
to place the media in standard media locations.
Can't create directory with mkdirs() - android
Use code below
File directory = new File(Environment.getExternalStorageDirectory() + java.io.File.separator +"Directory");
if (!directory.exists())
Toast.makeText(getActivity(),
(directory.mkdirs() ? "Directory has been created" : "Directory not created"),
Toast.LENGTH_SHORT).show();
else
Toast.makeText(getActivity(), "Directory exists", Toast.LENGTH_SHORT).show();
For Android API 23 (Marshmallow) and greater, we have to allow dangerous permissions otherwise our code will not work as we expected.
My Root Folder isn't create in android 10?
getExternalStorageDirectory
is deprecated in Android 10, you should check out new restrictions for accessing storage called Scoped Storage. for just Android 10 you can opt out and add requestLegacyExternalStorage
flag to manifest, but Scoped Storage will be mandatory in Android 11 and above
How to create a public folder/file in Android 10
when I use
File root = context.getExternalFilesDir(null);
The created files can only be seen by my app
They can be seen by any app that uses the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT
), if the user chooses the document that you place in that directory.
I write an app that creates data files that shall be picked up by other apps
Other apps have no access to external or removable storage on Android 10, except in the limited directories like getExternalFilesDir()
or via non-filesystem means (ACTION_OPEN_DOCUMENT
, MediaStore
).
How can I create a folder and files in Android 10 that are public?
Use getExternalFilesDir()
and related methods on Context
. Or, use ACTION_CREATE_DOCUMENT
or ACTION_OPEN_DOCUMENT_TREE
and use the Storage Access Framework. In either case, the resulting documents can be used by other apps that also use the Storage Access Framework.
Can't create a folder in external storage, Android
I suggest you to try Context.getExternalFilesDir()
instead of Environment.getExternalStorageDirectory()
Android 11 doesn't create app directory on external storage
Ok, I found a way to do it. Our app cannot create that folder but if we call for example getApplicationContext().getExternalFilesDir("");
Android will create that folder for us.
Quite stupid that if we want open dir which doesn't exist it will create it but when we want create it by ourself there is a problem. Android devs should makes our life easier but they making it more difficult with every update.
Related Topics
What's the Best Way to Share Data Between Activities
Check Orientation on Android Phone
How to Get a Resource Content from a Static Context
Why Maven Uses Jdk 1.6 But My Java -Version Is 1.7
Non-Static Variable Cannot Be Referenced from a Static Context
Avoiding Nullpointerexception in Java
Trusting All Certificates Using Httpclient Over Https
Difference Between a Variable, Object, and Reference
How to Get a Class Instance of Generic Type T
Remove HTML Tags from a String
How to Call a Soap Web Service on Android
Android: How to Use Alarmmanager
Populating a Listview Using an Arraylist
Java_Home and Path Are Set But Java -Version Still Shows the Old One
Sort a Map≪Key, Value≫ by Values
How to Create a Java String from the Contents of a File
What Are the Possible Values of the Hibernate Hbm2Ddl.Auto Configuration and What Do They Do