Copy the XX. DB file to the res \ raw directory of the eclipse Android project, as shown in 1. All files in the res \ raw directory are not compressed, so that files in the directory can be extracted directly.
Use the opendatabase method to open the database file. If the file does not exist, the system automatically creates the/sdcard/dictionary directory and sets XX in the res \ raw directory. copy the DB file to the/sdcard/dictionary directory. Opendatabase implementationCodeThis example uses dictionary. DB.
Private sqlitedatabase opendatabase () {try {// obtain dictionary. absolute path of the DB file string databasefilename = database_path + "/" + database_filename; file dir = new file (database_path); // If the/sdcard/dictionary directory exists, create this directory if (! Dir. exists () dir. mkdir (); // If the/sdcard/dictionary directory does not exist // dictionary. DB file, copy the file from the res \ raw directory to the // SD card directory (/sdcard/dictionary) if (! (New file (databasefilename )). exists () {// obtain the encapsulated dictionary. inputstream object of the DB file inputstream is = getresources (). openrawresource (R. raw. dictionary); fileoutputstream Fos = new fileoutputstream (databasefilename); byte [] buffer = new byte [8192]; int COUNT = 0; // start to copy dictionary. DB file while (COUNT = is. read (buffer)> 0) {FOS. write (buffer, 0, count);} FOS. close (); is. close ();} // open the dictionary in the/sdcard/dictionary directory. DB file sqlitedatabase database = sqlitedatabase. openorcreatedatabase (databasefilename, null); Return database;} catch (exception e) {} return NULL ;}
Several constants are used in the opendatabase method.ProgramThe Code is as follows:
Public class main extends activity implements onclicklistener, textwatcher {private final string database_path = android. OS. environment. getexternalstoragedirectory (). getabsolutepath () + "/Dictionary"; private final string database_filename = "dictionary. DB ";}