Android learning notes – saving files

Android devices have two file storage areas: internal storage and external storage. The name comes from the early Android. At that time, most Android devices provided two storage methods: built-in nonvolatile memory (internal storage) and removable storage, such as micro SD card (external storage). Some devices divide permanent memory into internal and external parts, so even if there is no external storage, there are still two kinds of storage space. The API method is the same whether there is external storage or not. For example, my mobile phone Xiaomi 2S has 16g ram and does not support the expansion of SD card. It divides the storage into internal and external parts, including 3.71g system storage (i.e. internal storage) and 10.16g memory device (i.e. external storage), as shown in the following figure:

Internal storage:

The saved files are always available and can only be accessed by your app by default. Uninstall the app. The system deletes all files of your app from the internal storage. The internal storage is applicable to the external storage of your files that you don't want users or other apps to access:

Not always available (the user may connect the external storage via USB and remove it from the device in some cases) is world readable, so some files may be read out of control. Uninstall the app and only delete the files stored in the getexternalfilesdir() directory

External storage is applicable to files that do not need storage restrictions, files you want to share with other apps, or files that users are allowed to access from the computer. App is installed in internal storage by default. App can be installed in external storage by specifying the value of Android: installlocation attribute.

Access to external storage: read and write:

Read:

You don't need any permissions to save files in internal storage. Your app always has read and write permissions in internal storage.

To save a file in internal storage:

Get the appropriate directory:

Getfilesdir() directory of APP file in internal storage eg: file file = new file (context. Getfilesdir(), filename);

Getcachedir() app directory of temporary cache file in internal storage call openfileoutput() to get fileoutputstream and write the file to internal directory eg:

Call createtempfile() to cache some files:

To save a file in external storage:

Since external storage is not always available, as mentioned above, the user may remove the SD card or connect the computer in USB mode. All users need to confirm that external storage is available before accessing. You can call getexternalstoragestate () to return the state of external storage. If the returned state is media_ Mounted, you can read and write files stored externally.

External storage can be accessed by users or other apps. We can save two kinds of files to external storage:

1. Public files

Files that can be freely accessed by users or other apps. When users uninstall the app, these files still exist. Call getexternalstoragepublicdirectory() to get the directory and save the public files to the external storage:

2. Private files

Files belonging to your app will be deleted when users uninstall them. Call getexternalfilesdir() to get the appropriate directory and save the private files to the external storage:

Delete file: myfile. Delete(); Delete the file saved in the internal storage: mycontext.deletefile (filename); When users uninstall the app, the Android system will delete the following files:

1. All files saved in internal storage 2. All files saved with getexternalfilesdir()

We should delete all files generated with getcachedir () and files that are no longer needed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>