13

Lately I updated my smartphone to Android 11 and I noticed that one of my app crashes on creating directory on external storage. I have read Android 11 documentation and here we are Starting in Android 11, apps cannot create their own app-specific directory on external storage. Quite strange because when I open Android/data dir there are a lot of applications folders so there is possibility to do it or maybe there are folders created by apps before updates ?

KyluAce
  • 933
  • 1
  • 8
  • 25
  • `Android/data` does not exist. You mean: `/storage/emulated/0/Android/data`. In this directory reside te app specific directories of all apps. Every app has access to its own directory. `/storage/emulated/0` is the root of external storage where your app cannot create directories... well unless you know how.. or use SAF. – blackapps Sep 24 '20 at 11:25
  • read this https://stackoverflow.com/a/66366102/9917404 – Thoriya Prahalad Feb 25 '21 at 09:50

3 Answers3

27

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.

KyluAce
  • 933
  • 1
  • 8
  • 25
  • but it will only create a directory in the android folder, not as @KyluAce wants in the external storage! – miraquee Jan 10 '21 at 12:30
  • Can't expain it correctly but in my case android folder was in external storage. Maybe in phones where there is no SD card extensions internal = external ? – KyluAce Jan 12 '21 at 12:42
  • 12
    I am just decided to stop developing for android as android is just for emulators not for real market as market is full of customised androids and apps never runs. Do whatever it will go silent. Privacy is wrongly implemented by big techs it is just for restricting developers, controlling free speech, controlling elections and democracies. Privacy is the one which steve jobs defined but Google and Facebook changed its definition for their own profits – Rushikant Pawar Feb 23 '21 at 08:37
  • what is the solution? please post solution rather then text – Anand Savjani Jun 24 '21 at 16:39
  • Android constantly forces us to make these updates and they do in fact go out of their way to break our apps, I have spent 3 days trying to fix this and have had hundreds of bad reviews because of this needless issue – Edmund Rojas Aug 26 '22 at 22:23
12

getApplicationContext().getExternalFilesDir(""); will give you your app's specific external storage directory and no other apps will be able to access its contents. As i believe, what was meant by saying

Starting in Android 11, apps cannot create their own app-specific directory on external storage

is that no other app-specific directory can be created on external storage but the one forementioned. I found this article somewhat useful.

Vladimir Rozin
  • 180
  • 2
  • 5
-2
            ContentValues values = new ContentValues();

            values.put(MediaStore.MediaColumns.DISPLAY_NAME, currentlyDownloadingFileObject.getFileName());
            values.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
            values.put(MediaStore.MediaColumns.BUCKET_DISPLAY_NAME, "my_file.mp4");
            values.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "YOUR_DIRECTORY_NAME_GOES_HERE");

            context.getApplicationContext().getExternalFilesDir(MediaStore.Video.Media.EXTERNAL_CONTENT_URI+"/"+getApplicationName(context));

            Uri newUri newUri = context.getContentResolver()
                    .insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);

         // values.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "YOUR_DIRECTORY_NAME_GOES_HERE");

You can store a file giving a RELATIVE_PATH in content values it will create the directory if not already created

Muhammad Zahab
  • 1,049
  • 10
  • 21