Android data storage (iii) external file storage

Source: Internet
Author: User

3. file data External Storage

Each Android device supports an external storage, which can be used to save files. This may be a mobile storage media (such as an SD card) or an internal (fixed) storage. Files stored in external storage are public and can be modified by users.

Note: All applications can access files stored on external storage devices, or even directly modify the files connected to a computer.

1. Check whether external storage devices are available

When you use an external storage device, you should always call the environment. getexternalstoragestate () method to check the availability of the external storage device. For example:

Boolean mexternalstorageavailable = false; Boolean mexternalstoragewriteable = false; string state = environment. getexternalstoragestate (); If (environment. media_mounted.equals (State) {// you have inserted an SD card and can read and write mexternalstorageavailable = mexternalstoragewriteable = true;} else if (environment. media_mounted_read_only.equals (State) {// the SD card has been inserted but is read-only. mexternalstorageavailable = true; mexternalstoragewritea Ble = false;} else {// other error status. External Storage devices may be in other equipment, but we need to know that in this case, we cannot read or write it. Mexternalstorageavailable = mexternalstoragewriteable = false ;}

This example only checks whether an external storage device can read and write data. It also has many other statuses, such as connecting to a computer, no device, or serious removal.

2. Files accessing external storage

If you use API Level 8 or a later version, you can use the getexternalfilesdir () method to open a file object, which represents the external storage directory of the file to be saved. Type indicates the subdirectory you want to access, such as environment. directory_music. If you want to access the root directory, enter null. If necessary, this method will create a suitable directory. By specifying the directory type, make sure that the android media scanner correctly classifies the files into the system (for example, the ringtones are marked as ringtones rather than music ). If you uninstall the application, all contents in the directory and directory corresponding to the application will be deleted.

If you use API level 7 or a lower version, use the getexternalstoragedirectory () method to open a file object, which represents the root directory of the external storage, and then write the data to the following directory:

/Android/data/<package_name>/files/

<Package_name> is the Java-style package name, for example, Com. example. Android. App. If your device is running API Level 8 or later and the application is uninstalled, the directory and all its contents will be deleted.

Example:

File file = new File(Environment.getExternalStorageDirectory() +filePathName);

In this way, the root path of the file is MNT/sdcard.

File file=getExternalFilesDir(null);

In this way, the file path is/mnt/sdcard/Android/data/package name/files.

Now that the sdcard path is obtained, you can use the IO stream to operate the file storage data.

However, when you use the second method to obtain the path, you must add the following permissions. In addition, the following permissions must be added during file operations.

Note: you must add permissions.
<! -- Create and delete a file on the SD card -->
<Uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/>
<! -- Write data permission to SD card -->
<Uses-Permission Android: Name = "android. Permission. write_external_storage"/>

3. Hide your files from a media browser

Add.For example.Image 1.Image 2 and so on

4. Save the file to be shared

If the stored files are not exclusive to the application and are not deleted when the application is uninstalled, save them to a public directory on the external storage. These directories are located in the root directory of the external storage, such as music/, pictures/, and ringtones.

In API Level 8 or later versions, use the getexternalstoragepublicdirectory () method to pass the required public directory type to this method, such as directory_music, directory_pictures, directory_ringtones, or other types. If necessary, this method will create an appropriate directory.

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

Music/--- the media scanner finds all media files in this directory as user music.

Podcasts/--- the media scanner uses all media files found in this directory as clip clips for audio/video clips.

Ringtones/--- the media scanner uses all media files found in this directory as ringtones.

Alarms/--- the media scanner uses all the media files found in this directory as the alarm sound.

Pictures/--- all pictures (excluding those taken with a camera ).

Movies/--- all movies (excluding videos shot with a camera ).

Download/--- Other downloaded content.

5. Save cache files

If you use API Level 8 or a later version, use getexternalcachedir () to open a file object, which represents the external storage directory for storing cached files. If you uninstall the application, these files will be automatically deleted. However, during the application's survival, you should manage these cached files by yourself, and delete these cached files when you do not need them to reserve the storage space.

If you use API level 7 or a lower version, you need to use the getexternalstoragedirectory () method to open a file object, which represents the root directory of the external storage, and then write the cached data to the following directories:

/Android/data/<package_name>/cache/

<Package_name> is the Java-style package name, for example, 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.