The file for Android data storage

Source: Internet
Author: User

Android uses a disk-based file system similar to other platforms (disk-based file systems), and the previous article uses sharedperference for data storage, which you can use with file this time. The file object is well-suited for reading and writing data that flows sequentially, and can be used to read and write image files or data exchanged in a network.

Storage location

All Android devices have two file storage areas: "Internal" and "external" storage. The two names come from an earlier Android system, when most devices have built-in immutable memory (internal storage), followed by a storage unit like the SD card (external storage) that can be unloaded. Later, some devices made the "internal" and "external" parts into non-offloaded built-in storage, although this whole block was logically divided.

Internal Storage: always available, the file here is only accessible by its own app, and when the user uninstalls the app, the system will clean up the relevant files in the Internal; Nternal is the best storage area you want to ensure is not accessed by users and other apps.

External Storage: is not always available, because the user can choose to use this as a USB storage mode, so that it can not be accessed; is accessible to everyone, so the files saved here are lost access control, and when the user uninstalls your app, the system only deletes the relevant files under the external root directory (GETEXTERNALFILESDIR); External is the best storage area where you don't need strict access rights and you want these files to be shared by other apps or to allow users to access them from your computer.

Internal Storage Save

When you save to your phone's internal, there are two ways to remove the file directory:

Getfilesdir (): Returns a file that represents the internal directory of the app.

Getcachedir (): Returns a file that represents the internal cache directory for the app. You need to make sure that the files in this directory can be deleted as soon as they are no longer needed, and allocate a reasonable size, such as 1MB. If the system has insufficient internal storage space, it chooses to delete the cache file itself.

In this way, a file is created directly from the Openfileoutput, under the corresponding path:

String filename = "testfile"; String string = "flyelephant!"; FileOutputStream OutputStream; try {outputstream = openfileoutput (filename, context.mode_private); Outputstream.write (String.getbytes ()); Outputstream.close (); } catch (Exception e) {e.printstacktrace ();}

Of course, if you want to save some cache files, you need to call the system's Getcachedir () method:

File file; try {String filename= "cachetest"; file = File.createtempfile (FileName, ". txt", MainActivity.this.getCacheDir ());} catch (IOException e) {//Error while creating file}
External Storage Save

Just saved to internal when nothing is configured, you need to save to the outside when you need to configure read and write permissions, read the permissions read_external_storage, write permissions: read_external_storage:

    <uses-permission android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission Android : Name= "Android.permission.READ_EXTERNAL_STORAGE"/>

First look at the code saved to the external store:

if (Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File File = new file ( Environment.getexternalstoragedirectory (), "66.txt"); try {fileoutputstream fos = new FileOutputStream (file); O Bjectoutputstream Oos = new ObjectOutputStream (FOS), Oos.writeobject ("flyelephant");//write Fos.close (); Close output stream} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) { TODO auto-generated catch Blocke.printstacktrace ();} LOG.I ("Com.example.googlefile", File.getabsolutepath ());

First, you need to determine whether the SD card is available, because external storage may be unavailable, such as the SD card is unplugged, then you should check the availability before accessing. You can query the status of external storage by executing getexternalstoragestate (). If the returned state is media_mounted, then you can read and write. (at the beginning of the absence of judgment, has not found the cause of the error)

See this after getting finished with the above stored in the internal storage device process, the following is not much to say, the last point is that the path is in the/mnt/sdcard directory, if it is a private file, do not allow external access, the directory should be in

The/mnt/sdcard/android/data/is recorded in the package.

File storage is relatively simple, in addition to the file delete it, save to external:

File. Delete ()

Delete in internal:

MainActivity.this.deleteFile ("Testfile")

The file for Android data storage

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.