Android data storage (2)

Source: Internet
Author: User
Tags sqlite query

Use External Storage

Each Android-compatible device supports a shared external storage used to save files. This memory can be a removable storage medium (such as an SD card) or internal (non-removable) storage. Files stored in external storage are completely shared and can be modified when USB storage is enabled to transfer files to a computer.

Warning If an application mounts the external storage to a computer or removes the storage medium, the external files will not be displayed and the files stored in the external storage will not be safe. All applications can read/write files stored in external memory, and users can delete them.

Check the availability of storage media

Before using external storage, always call the getexternalstoragestate () method to check whether the storage media is available. The storage media may be attached to a computer in a lost or read-only state. The following describes how to check availability:

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

If
(Environment. media_mounted.equals (State ))
{
// We can read and write the media
Mexternalstorageavailable
= Mexternalstoragewriteable
= True;
}
Else if
(Environment. media_mounted_read_only.equals (State ))
{
// We can only read the media
Mexternalstorageavailable
=
True;
Mexternalstoragewriteable
=
False;
}
Else {
// Something else is wrong. It may be one of your other states, but all we need
// To know is we can neither read nor write
Mexternalstorageavailable
= Mexternalstoragewriteable
= False;
}

This example checks whether external memory can be used for read/write. You may also want to check other statuses of the getexternalstoragestate () method, such as whether the storage media is shared (connected to a computer), completely lost, or maliciously deleted. With these statuses, the application can provide more notification information when it needs to access the storage media.

Access files on 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. This method requires a type parameter, which specifies the type of the desired subdirectory, such as directory_music and directory_ringtones (if null is passed, the root directory of the application's file directory will be obtained ). 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.

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.

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.

Use Database

Android provides full support for SQLite data. Any database created in the application can be accessed by the class name, but cannot be accessed outside the application.

We recommend that you create a new SQLite data by creating a subclass of the sqliteopenhelper class and overwriting the oncreate () method, in this method, you can run the SQLite command to create tables in the database, for example:

Public
Class dictionaryopenhelper
Extends sqliteopenhelper
{

Private
Static final
Int database_version
= 2;
Private
Static final
String dictionary_table_name
= "Dictionary ";
Private
Static final
String dictionary_table_create
=

"Create Table"
+ Dictionary_table_name
+ "(" +
Key_word
+
"Text," +
Key_definition
+
"Text );";

Dictionaryopenhelper (Context
Context ){
Super (context,
Database_name, null, database_version );
}

@ Override
Public
Void oncreate (sqlitedatabase dB)
{
Db.exe csql (dictionary_table_create );
}
}

Then, use the constructor of the sqliteopenhelper implementation class to obtain an instance. Call the getwritabledatabase () and getreadabledatabase () Methods to read and write the database respectively. Both return a sqlitedatabase object, which represents the database and provides the SQLite operation method.

Sqlitedatabase. Query () is used to perform the SQLite query operation. It receives various query parameters, such as the table to be queried, projection, selection conditions, columns, and groups. For complex queries that require column aliases, use the sqlitequerybuilder class, which provides several convenient methods for building queries.

Each SQLite query returns a cursor object pointing to all rows of the query results. The cursor object can always navigate the query results of the database and read the data of the current row and column.

Database debugging

The android SDK includes sqlite3 database tools that can be used to view table content, run SQL commands, and execute other SQLite data functions.

Use Network Connection

When the Network is available, you can store and obtain data based on Web Services. Use the classes in the following packages for network operations.

Java.net .*;

Android.net .*

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.