File Storage in Android

Source: Internet
Author: User

About the file storage of Android, here is the file class in Java, according to the idea of object-oriented, the operation of the file in Java has also been encapsulated object, the operation of the file class is the files class, file provides a rich API for file operation, such as common CreateNewFile (), mkdir (), mkdirs (), exists (), Isfile (), Isdictory (), Renameto (), delete (), GetName (), GetPath () .... Method is more, the reader can be viewed through the API document, the same set of file operations in Android also applies.

Examples of several commonly used APIs are described below.

1.File Constructors

File File=new file (String pathname);
This creates a new file instance by converting the given pathname string to an abstract path name.
Then executes File.createnewfile (); If the file exists, the creation fails, the creation does not exist, and a Boolean value is returned

File file=new file (file parent,string child);
This creates a new file instance based on the parent abstract path name and the child pathname string.
Execute File.createnewfile (); If the parent abstract path name does not exist, or if the file has been created, it cannot be created successfully.

File File=new file (String parentpath,string child);
Creates a new File instance based on the parent pathname string and the child pathname string.
Execute File.createnewfile (); If the parent abstract path name does not exist, or if the file has been created, it cannot be created successfully.

2.File method

    • CreateNewFile (); If the file already exists, the creation fails, or it is created successfully, and there are other possible
    • mkdir (); Create a folder, Note: If the parent directory that created the destination folder does not exist, a failure is created
    • Mkdirs (); Create a multi-level directory where the parent file directory does not exist, and the parent directory is created automatically.
    • Isfile (); Determine if it is a file
    • Isdictory (); Determine if it is a folder
    • Delete () remove files or folders, note that if you delete folders, there are files or folders in the folder, you cannot delete them, meaning you can only delete an empty folder.
    • exists (); Determine if a file or folder exists
    • GetName (); Gets the name of the file or folder
    • GetPath (); Get relative path
    • GetAbsolutePath (); Get absolute path
    • Length () to get the file or folder

。。。 There's a lot more to be described.

Let's go to the Chase and review the file store in Android.

Before describing how to read files under Android, it is important to understand the data storage rules under the Android platform. Under other operating systems such as Windows, applications can access or modify resources such as files under other applications freely or on the basis of specific access rights, and all of the data in an application is private under the Android platform.

When the application is installed in the system, its package will have a folder to hold its own data, only this application can write permission to this folder, the private folder is located in the Android System/data/data/< application Package name > directory, No other application can write data to this folder. In addition to storing the private data folder, the application also has write access to the SD card.

The file I/O method allows you to store data directly into your phone, which by default cannot be accessed by other applications. The Android platform supports file I/O operations under the Java platform, primarily using the two classes of FileInputStream and FileOutputStream for file storage and reading. There are two ways to get these two class objects.
One: The first way is to create directly from the constructor, as implemented in the Java platform, if you need to write data to the end of an open file, you can use the constructor fileoutputstream (file file, Boolean append) to The append is set to true to implement. However, it is important to note that the FileNotFoundException exception is thrown if the file does not exist or is not writable when the FileOutputStream object is obtained in this way.

Two: The second way to get FileInputStream and FileOutputStream objects is to call Context.openfileinput and Context.openfileoutput two methods to create them. In addition to these two methods, the context object provides several other methods for manipulating the file, as shown below

Method description
Openfileinput (String filename) opens the specified private file in the application's private directory to read in the data, returning a FileInputStream object
Openfileoutput (String filename,int mode) opens the specified private file under the application's private directory to write the data, returning a FileOutputStream object to create the file if it does not exist.
FileList ()
Searches for private files under the application's private folder, returns a string array of all file names
DeleteFile (String filename) Deletes the file of the specified file name, returns true successfully, and returns false
When you use the Openfileoutput method to open a file to write data, you need to specify an open mode. The default is zero, which is mode_private. The meanings of the different patterns correspond to the following:
Constant meaning
Mode_private
Default mode, the file can only be accessed by the application that called the method
Mode_append
If the file already exists, continue to write the data to the end of the file, instead of overwriting the original data.
Mode_world_readable
Gives all applications permission to read the file.
Mode_world_writeable
Gives all applications permission to write to the file.
But Android 4.2 started, Android is not recommended to use, mode_world_readable, mode_world_writeable these two modes.

When the app saves files or caches data on the phone, I think the following points should be observed:
    • Do not arbitrarily occupy the user's built-in storage.
    • Do not arbitrarily create a new directory on the SD card, you should place your app package name corresponding to the extended storage directory, uninstall the app can be automatically cleared.
    • There is an upper limit on the amount of disk space that is occupied and is purged according to a certain policy.
What file directories are under Android to apply private storage (built-in storage)

How to obtain:
Context.getfiledir (): Gets the file directory under the built-in storage that can be used to save sensitive data such as user personal information that cannot be exposed to other applications
Context.getcachedir (): Gets the cache directory under the built-in storage, can be used to save some cache file slices, when the built-in storage space is not enough to automatically clean up the system (however, how much, the strategy of the purge I did not find out.) )
Absolute path:
Context.getfiledir ():/data/data/app Package name/files/
Context.getcachedir ():/data/data/app Package name/cache/
Write permission: No application required

This is the phone's built-in storage, no root of the phone can not be used in the File Manager and other tools to view. And the data will be deleted along with the user uninstalling the app. These two directories actually correspond to the purge data and clear cache in your app-> storage space, application------

Apply extended Storage (SD card)

How to obtain:
Context.getexternalfilesdir (): Get the file directory on the SD card
Context.getexternalcachedir (): Get the cache directory on the SD card
Absolute path:
Context.getexternalfilesdir (): sdcard/android/data/app Package name/files/
Context.getexternalcachedir (): sdcard/android/data/app Package name/cache/
Write permission:
API < 19: Need to apply
API >= 19: No application required
Since it is the directory on the SD card, it can be read by other applications, so this directory should not store the user's sensitive information. As with the above, the files here will be removed as the app is uninstalled, or it can be cleared manually by the user in the Settings screen.

Public Storage (SD card)

Access method: Environment.getexternalstoragedirectory ()
Absolute path: sdcard/You set the folder name/
Write permission: Need to apply

If our app needs to store some public files, even the files we want to download can be used by other apps even after our app has been deleted, then you can use this directory. This directory is always required for SD Write permission.
Where should I put the files under the Android6.0?

With the introduction of the previous section, it is very clear, according to the first mentioned rules, in fact, if only to do a simple picture caching work, then we should put the picture cache to/data/data/application package name/cache/or sdcard/android/data/application package name/ cache/, because in the 6.0 system (API23), you can write files to both directories without requesting permission. and/data/data/application package name/cache/directory, is built-in storage of the application private cache directory, when the system space is not enough to be automatically cleared, for the picture cache is also a good management strategy, but Google recommends that we better still implement cache cleanup management, For example, with Disklrucache.

Reference article: http://unclechen.github.io/2016/03/06/Android6.0%E6%9D%83%E9%99%90%E9%80%82%E9%85%8D%E4%B9%8BSD%E5%8D%A1% e5%86%99%e5%85%a5/

File Storage in Android

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.