Android data Store (iii) FILE data external storage

Source: Internet
Author: User
Tags file permissions

Three File data external storage

Each Android device supports a shared "external store" that you can use to save files. This could be a removable storage medium (such as an SD card) or an internal (fixed) store. Files saved to the external store are public and can be modified by the user.

Note: Files on external storage devices, all applications can access it, and can even be connected directly to the computer to modify.

1, check the external storage device is available

When you use an external storage device, you should always call the Environment.getexternalstoragestate () method first to check the availability of the external storage device. Like what:


Boolean mexternalstorageavailable = false;
Boolean mexternalstoragewriteable = false;
String state = Environment.getexternalstoragestate ();

if (Environment.MEDIA_MOUNTED.equals (state)) {//The SD card has been inserted and can read and write
    mexternalstorageavailable = Mexternalstoragewriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) {//SD card has been inserted, but is read-only
    mexternalstorageavailable = true;
    Mexternalstoragewriteable = false;
} else {
    //other error status. The external storage device may be in other equipment, but we need to know that in this case we cannot read or write to it. 
    mexternalstorageavailable = Mexternalstoragewriteable = false;
}

This example only checks whether the external storage device is readable or not, and it has many other statuses, such as connecting to a computer, having no device, or removing a serious removal, and so on.


2, access to external storage files

If you use API Level 8 or higher, you can use the Getexternalfilesdir () method to open a file object that represents the external storage directory where the file should be saved. Type indicates what subdirectories you want to access, such as Environment.directory_music, and if you access the root directory, pass in NULL. If necessary, this method creates the appropriate directory. By specifying the type of directory, make sure that the Android media scanner classifies the files correctly into the system (for example, ringtones are labeled as ringtones, not music). If the user unloads the application, all content in the corresponding directory and directory will be deleted.

If you use API Level 7 or lower, use the getExternalStorageDirectory () method to open a file object that represents the root of the external storage, and then you should write the data to the following directory:

/android/data/<package_name>/files/

<package_name> is a Java-style package name, such as: Com.example.android.app. If the user's device is running API Level 8 or higher, and the application is uninstalled, the directory and all of its contents will be deleted.

Example:

File File = new file (Environment.getexternalstoragedirectory () +filepathname);
This way the file root path is Mnt/sdcard


File file=getexternalfilesdir (NULL);
This way to get the file and the path for the/mnt/sdcard/android/data/package name/files

Now that you have the SDcard path, you can use IO streaming to manipulate the file to store the data.

However, when you get the path in the second way, be sure to add the following permissions. Also, the following permissions are added to the file operation.

Note that you also need to add permissions
<!--Create and delete file permissions in SD cards-->
<uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--write data permissions to SD card-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

3, the media browser to hide your files

In front of the file name plus . For example . Picture 1 . Picture 2 and so on



4, save the file should be shared

If the saved files are not proprietary to the application and are not deleted when the application is unloaded, they are saved to a public directory on the external storage. These directories are located in the root directory of the external storage, such as music/, pictures/, ringtones/, and so on.

In API Level 8 or later, the Getexternalstoragepublicdirectory () method is used to pass the required public directory types to this method, such as Directory_music, Directory_pictures, Directory_ringtones or other type. If necessary, this method creates the appropriate directory.

If you use API Level 7 or lower, use the getExternalStorageDirectory () method to open a file object that represents the root of the external storage, and then save the shared file to one of the following directories:

music/---Media scanner to find all media files in this directory as user music.

podcasts/---Media scanner to use all media files found in this directory as audio/video clip clips.

ringtones/---Media scanners as ringtones for all media files found in this directory.

alarms/---Media scanner to make all the media files found in this directory sound as the alarm clock.

pictures/---All the pictures (not including those taken with the camera).

movies/---All the movies (excluding those that were filmed with a video camera).

download/---Other downloaded content.

5. Save Cached Files

If you use API Level 8 or higher, use Getexternalcachedir () to open a file object that represents the external storage directory where the cached files are saved. If you uninstall the application, the files are automatically deleted. However, these cached files should be managed on their own during the lifetime of the application, and in order to preserve storage space, these cached files should be deleted when they are not needed.

If you use API Level 7 or lower, you use the getExternalStorageDirectory () method to open a file object that represents the root of the external storage, and then writes the cached data to the following directory:

/android/data/<package_name>/cache/

<package_name> is a Java-style package name, such as: Com.example.android.app.

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.