There is a document in the official Android development documentation that describes how to save application data, but I have used many programs from well-known to unknown.) The processing is not perfect, or the Android development team did not save the data of their applications as recommended. After some tested software is uninstalled, the Directory of these software files is retained in SDCard. After a long time, many directories need to be manually cleaned up, it is hard to confirm that the directories that are still in use are useless, which brings troubles to users. Here we will discuss how to properly Save the application data.
There are two types of data: Application-related data and application-independent data. Here we will introduce them separately:
Application-independent dataIt is the data that users are concerned about. No matter whether your application is on a user's device or not, the data users want to keep. The data includes: photos taken with a camera, files downloaded with a browser (download tool), personalized ringtones made by users, etc.
Assume that you have developed a photo application (for example, a 360 camera). The photos you have taken and processed with your application are unrelated to the application, if the user uninstalls your application, the user still expects to keep the photos they have taken and processed. The photos here should be saved to the DIRECTORY_PICTURES directory according to Android's official suggestions. The directory is stored through Environment. getExternalStoragePublicDirectory (String type). You can create a directory named after your program to save the data. For a list of other supported directories, see here.
Application-related data: This type of data is only related to your application. If your application is deleted by the user, there is no reason for the data to exist on the user device. Such data includes database files, attribute configuration files, application cache files, and so on. This type of data can be stored in many ways. For details, refer to here.
Here we will only discuss the introduction of a pseudo-private data API saved to an external bucket in API 8 (Android 2.2), which is obtained through the getExternalFilesDir () function, you can also set parameters for obtaining various types of data, such as DIRECTORY_MUSIC and DIRECTORY_RINGTONES (if the parameter is null, the following directory of your application data is returned ). For example, an application package named org. goodev. test application, through the function getExternalFilesDir (Environment. directory obtained by DIRECTORY_MOVIES is/storage/sdcard0/Android/data/org. goodev. test/files/Movies.
Careful readers have found that the data directory is SDCard (an external storage device, possibly a built-in SDCard, such as Nexus S) under the root directory named Android, there is a data directory under this directory to save application data. In this data directory, the data of each application in the device is saved and named according to the package name, if the parameter is set to null, the returned directory is/storage/sdcard0/Android/data/org. goodev. test/files.
Why is this directory a pseudo private data directory? In more than 2.2 of systems, data stored in this directory will also be deleted when your applications are uninstalled by users; by default, the multimedia scanner does not scan images, MP3 files, and other multimedia files in this directory. From this perspective, this directory is the private data directory of the application. Because this directory exists in an external bucket, any other application with the permission to read and write the external bucket can access your application data-from this perspective, the data is not private. So we call it pseudo private data.
Why does Android 2.2 introduce this pseudo-private data storage method? Isn't the application data so insecure? Which data storage method is suitable for storing? Next we will analyze these problems one by one:
Why is this storage method introduced?
Some Android applications may generate a lot of file data to be stored during use, and the data needs to be deleted when the application is uninstalled, in the way before 2.2, data of this requirement can only be stored in the internal storage space, and the internal storage space of the system is limited. In order to allow users to install more applications in the limited storage space, therefore, this storage method is available.
Is the data stored in a pseudo-Private directory insecure?
Of course, this is not safe. Any application may access this data. Therefore, only the data that is not very sensitive is stored here, because the data in these directories may be deleted by users or other applications, you must first check whether the data exists and is valid when using the data, if it is invalid, You need to download and use it again.
What kind of data is this storage method suitable?
For example, an image browsing application uses this directory to save thumbnails of system images to improve the smoothness of the application's browsing images, or a library application to save cover pictures of books.
If your application runs on an old device (earlier than 2.2), save the file to the/Android/data/files/directory as recommended, in this way, after the device version is updated, the data will be deleted as the application is detached.
Legacy problems
Of course, there are also some controversial data. For example, should images and custom emoticons received by IM chat software be stored in SDCard or the pseudo private data storage directory described above? If you have uninstalled the chat software, do you want to continue saving the images and custom emoticon images they have received? The answer to this question may be different for different users. What should we do ??
I personally recommend that you guide the user to set where the data is stored when the user uses the program for the first time? The data still exists after the application is detached.) or, the data is automatically deleted after the application is detached in the external storage directory of the application ). This method is similar to downloading some PC software. For example, some software may fill out a dialog box when it is uninstalled to allow users to choose whether to retain user data. However, this function is not provided for uninstalling an application in the Android system. Therefore, you can only know this setting item when the application is used for the first time.
PS: I am most disgusted with the fact that some applications have downloaded many small images to SDCard, then, open the image library application and you will find that there are many such small pictures, such as the person's profile picture, the thumbnail picture of the item, and the book cover picture ), such data should not appear in the user's Image Library application. This problem does not occur if you save the data to the preceding pseudo-private data directory. To avoid this problem, I have to create the ". nomedia" file in various directories. Very tedious!