Android 11 doesn't create app directory on external storage

Viewed 24846

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 ?

3 Answers

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.

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.

            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

Related