Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't create directory in Android 10

Tags:

java

android

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():

   File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Pastebin");                     if (!f.isFile()) {                         if (!(f.isDirectory())) {                                success =  f.mkdir();                         } 

Here, the variable success is always false which means the directory isn't created.

Using Files.createDirectory():

   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();                             }                         } 

which causes this exception:

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) 

I've implemented the run-time permissions and

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

are all set.

like image 709
pz64_ Avatar asked Oct 14 '19 15:10

pz64_


People also ask

How do I create a folder on Android 10?

Drag the app icon and drop it onto another app. This piles the apps on top of one another, which creates a folder.

How do I create a folder in Android root directory?

Show activity on this post. File mydir = context. getDir("mydirectory", Context. MODE_PRIVATE); //Creating an internal dir; File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.


1 Answers

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 as getExternalFilesDir(), 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 and ACTION_CREATE_DOCUMENT.

  • If your content is media, you can use MediaStore to place the media in standard media locations.

like image 93
CommonsWare Avatar answered Sep 30 '22 19:09

CommonsWare