You can copy the dictionary. 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 the dictionary in the Res/raw directory. copy the DB file to the/sdcard/dictionary directory. The implementation code of the opendatabase method is as follows:
Code
Private sqlitedatabase opendatabase () <br/>{< br/> try <br/>{< br/> // obtain a dictionary. absolute path of the DB file <br/> string databasefilename = database_path + "/" + database_filename; <br/> file dir = new file (database_path ); <br/> // If the/sdcard/dictionary directory exists, create this directory <br/> If (! Dir. exists () <br/> dir. mkdir (); <br/> // If the directory does not exist in the/sdcard/dictionary directory <br/> // dictionary. DB file, copy the file from the res/raw directory to the <br/> // SD card directory (/sdcard/dictionary) <br/> If (! (New file (databasefilename )). exists () <br/>{< br/> // obtain the encapsulated dictionary. inputstream object of the DB file <br/> inputstream is = getresources (). openrawresource (R. raw. dictionary); <br/> fileoutputstream Fos = new fileoutputstream (databasefilename); <br/> byte [] buffer = new byte [8192]; <br/> int COUNT = 0; <br/> // Start copying dictionary. DB file <br/> while (COUNT = is. read (buffer)> 0) <br/>{< br/> FOS. write (buffer, 0, count); <br/>}< br/> FOS. close (); <br/> is. close (); <br/>}< br/> // open the dictionary in the/sdcard/dictionary directory. DB file <br/> sqlitedatabase database = sqlitedatabase. openorcreatedatabase (<br/> databasefilename, null); <br/> return database; <br/>}< br/> catch (exception E) <br/>{< br/>}< br/> return NULL; <br/>}
Several constants are used in the opendatabase method. These constants are defined in the main class of the program. The Code is as follows:
Code
Public class main extends activity implements onclicklistener, textwatcher <br/>{< br/> private final string database_path = android. OS. environment <br/>. getexternalstoragedirectory (). getabsolutepath () <br/> + "/Dictionary"; <br/> private final string database_filename = "dictionary. DB "; <br/>}
From http://www.eoeandroid.com/thread-34883-1-1.html