Android Learning Notes-save file (saving files) _android

Source: Internet
Author: User
Tags readable save file

There are two types of file storage areas for Android devices:

internal storage and external storage ("internal" and "external" storage).

The name comes from early Android, when most Android devices offer two types of storage: built-in nonvolatile memory (internal storage) and removable Storage such as micro SD cards (external storage).
Some devices divide permanent memory into both internal and external parts, so there are two storage spaces, even without external storage. Regardless of external storage, the API approach is the same.
such as my mobile phone millet 2S is 16G size of RAM, does not support the expansion of SD card. It divides storage into both internal and external parts, 3.71G system storage (i.e., internal storage), 10.16G memory devices (i.e. external storage), as shown in the following illustration:

Internal storage:

is always available
Saved files can only be accessed by your app in the default way
Uninstall app, the system deletes all your app files from internal storage

Internal storage applies to your files that you don't want users or other apps to access

External storage:

Not always available (users may connect external storage to USB and in some cases remove from device)
is globally readable (world-readable), so some files may be read out of control
Uninstall the app and delete only the files you have stored in the Getexternalfilesdir () directory

External storage applies to files that do not require storage limits and files that you want to share with other apps or that allow users to access them by computer
The app is installed by default in internal storage, allowing the app to be installed in external storage by specifying the Android:installlocation property value.

To obtain external storage permissions:
Read and write:

Copy Code code as follows:

<manifest ...>
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
...
</manifest>

Read:

Copy Code code as follows:

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

Storing files internally does not require any permissions, and your app always has read and write access to the internal store.

To save a file in the internal store:

Get the appropriate directory:

Getfilesdir () directory of app files in internal storage
eg
File File = new file (Context.getfilesdir (), filename);

Getcachedir () The directory in which the app temporarily caches files in the internal store

Call Openfileoutput () get FileOutputStream write file to internal directory
eg

Copy Code code as follows:

String filename = "myfile";
String string = "Hello world!";
FileOutputStream OutputStream;

try {
OutputStream = openfileoutput (filename, context.mode_private);
Outputstream.write (String.getbytes ());
Outputstream.close ();
catch (Exception e) {
E.printstacktrace ();
}

Call Createtempfile () to cache some files:

Copy Code code as follows:

Public File gettempfile (context, String URL) {
File file;
try {
String fileName = uri.parse (URL). getlastpathsegment ();
File = File.createtempfile (FileName, NULL, Context.getcachedir ());
catch (IOException e) {
Error while creating file
}
return file;
}

To save a file in an external store:

Since external storage is not always available, as mentioned above, the user may have removed the SD card or USB mode to connect to the computer. All need to confirm that external storage is available before accessing.
You can call getexternalstoragestate () to return the state of the external store, and if you return media_mounted, you can read and write files that are stored externally.

Copy Code code as follows:

Determine if external storage can read and write
public Boolean isexternalstoragewritable () {
String state = Environment.getexternalstoragestate ();
if (Environment.MEDIA_MOUNTED.equals (state)) {
return true;
}
return false;
}

Determine if the external store can be read at least
public Boolean isexternalstoragereadable () {
String state = Environment.getexternalstoragestate ();
if (Environment.MEDIA_MOUNTED.equals (state) | |
Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) {
return true;
}
return false;
}

External storage can be accessed by users or other apps, and we can save two files to an external store:

1. Public files

Files that are free to be accessed by users or other apps, and are still present when the user unloads the app.
Call Getexternalstoragepublicdirectory () to obtain the directory, save the public files to the external store:

Copy Code code as follows:

Public File Getalbumstoragedir (String albumname) {
Get user's public picture directory
File File = new file (Environment.getexternalstoragepublicdirectory (
environment.directory_pictures), albumname);
if (!file.mkdirs ()) {
LOG.E (Log_tag, "Directory not created");
}
return file;
}

2. Proprietary files (private file)

Files that belong to your app that will be deleted when the user unloads them.
Call Getexternalfilesdir () get the appropriate directory, save the private file to the external store:

Copy Code code as follows:

Public File Getalbumstoragedir (context context, String Albumname) {
Get the application private picture directory
File File = new file (Context.getexternalfilesdir (
environment.directory_pictures), albumname);
if (!file.mkdirs ()) {
LOG.E (Log_tag, "Directory not created");
}
return file;
}

To delete a file:
Myfile.delete ();
To delete files that are saved in the internal store:
Mycontext.deletefile (FileName);
When users uninstall the app, the Android system deletes the following files:

1. All files stored in the internal store
2. All files saved with Getexternalfilesdir ()

We should remove all files generated with Getcachedir () and files that are no longer needed

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.