Why are Android folders so cluttered?

I believe that all users of Android phones will have this question, open their own file manager, is simply a face meng, why a hundred thousand instant in my mind.

Why are there so many folders?

What the hell are these folders for?

I have obviously uninstalled this APP, why there are still residual files?

Can I delete these folders that I don’t understand?

What if my phone turns brick after I delete it?

.

Under that question, the first sentence in the top vote-getter’s answer hit the nail on the head: Because developers don’t follow the spec.

This year, Android 10 has introduced a new Scoped Storage. It happens that The Android Dev Simmit has also mentioned related content. I will introduce the Scoped Storage to you based on the presentation at the conference and my own understanding. And what changes, as developers, we need to be aware of. If you don’t follow it, follow the subscription duck! If you find any of these articles interesting, be sure to share, retweet, comment and like them!

Let’s go back to the original question: Why are Android folders so messy? That’s because once the App has the WRITE_EXTERNAL_STORAGE permission, it can create countless folders in your root directory, because the developer didn’t follow the rules.

Correct specification

According to the official documentation, there are four ways to store data and files: internal storage, external storage, SharedPreferences and database. Today’s focus is on the two in the red box, internal storage and external storage. In case you are confused about the definition and purpose of these two stores, LET me give you a quick review.

Internal storage

The internal storage directory is divided into four parts, as shown in the figure above. However, we often use file directories and cache directories, which are available in Kotlin via filesDir and cacheDir respectively.

Internal storage is used to store the private files of an application, which are usually necessary for the function of the application.

Other applications (and users) cannot access these files unless they have Root access. As such, internal storage is ideal for storing internal application data that users do not need to access directly. In the file system, the system provides a private directory for each application, where you can organize any files your application needs. When a user uninstalls your application, the files saved in internal storage are removed.

At the same time, the cache directory in the internal storage helps us hold some data temporarily rather than permanently. When the internal storage space is insufficient, the system deletes these cache files to reclaim space.

The internal storage directory is /data/data/< package name >/files

The path of the internal storage cache directory is /data/data/< package name >/cache

External storage

The external storage path is /storage/Android/data/< package name >/files

According to the above definition, either internal or external storage should only store data associated with our own App. For example, internal storage is used to save user information, and external storage is used to save files in special formats that can only be opened by this App. Note that when a user uninstalls the App, both the internal and external storage will be deleted automatically.

Files generated for user behavior, such as downloaded images, saved videos, etc. Google requires us to save these files in the system’s public directory so that other apps can also access them, such as Pictures and Downloads. Here, we divide the files generated by user behaviors into two categories: multimedia files and other files. It is officially recommended that multimedia files be stored in the system’s special directory: Music, Movies, Pictures, etc. All other files should be saved in the Downloads directory.

So I want to ask the App developers, did you read the official documentation when you were developing the App?

Before Android 10.0, you need to obtain the WRITE_EXTERNAL_STORAGE permission to store files. App can through the Environment. External.getexternalstoragedirectory () under the root directory to create a file, but (to highlight!) Not after Android 10.0, guess what?

Android 10 Scoped Storage

Executing the code above to create folders won’t do anything at all, and the App won’t be able to create folders anymore. In Android Q, we don’t need to declare any permissions for internal and external storage.

With that said, let’s take a look at the modifications to Scoped Storage and what we need to pay attention to. Here are some important points for your reference:

Of course, Scoped Storage is not mandatory immediately, Google has given developers time to fully adapt.

  1. TargetSDK = 29, the default open Scoped Storage, but can be added in the manifest requestLegacyExternalStorage = true to shut down

  2. TargetSDK < 29, the default is not open Scoped Storage, but can be added in the manifest requestLegacyExternalStorage open = false

If your App is a file manager or data backup App, you need to submit your application information on Google Play and get whitelisted permission to access files other than your own App.

The information on Scoped Storage is messy and fragmentary, but there are only two points to focus on:

  1. Multimedia files need to be accessed using the MediaStore API

  2. Other files need to be accessed using Storage Access Framework apis

Therefore, we also need to pay close attention to the changes and updates of these two apis in the adaptation work of our App. Since I have not read the documents of these two apis carefully for adaptation, there is no superfluous adaptation code to share with you for the time being, so as to avoid misleading use. If any students have already started adaptation work, Please write an article and write to me to share your experience in the process of adaptation.

In tomorrow’s push, I will bring you the latest progress about the present and future of Fragment. If you don’t follow it, please remember to follow me and my official account. If you find any of these articles interesting, be sure to share, retweet, comment and like!!

I’m Wanbo, come on!