You can copy the Dictionary.db file to the Res\raw directory in the Eclipse Android project, as shown in 1. All files in the Res\raw directory are not compressed, so that the files in that directory can be extracted directly.
Use the OpenDatabase method to open the database file, and if the file does not exist, the system automatically creates the/sdcard/dictionary directory and copies the dictionary.db files in the Res\raw directory to the/sdcard/ The dictionary directory. The implementation code for the OpenDatabase method is as follows:
Code
Private Sqlitedatabase OpenDatabase ()
{
Try
{
Get the absolute path to the dictionary.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 it does not exist in the/sdcard/dictionary directory
dictionary.db file, copy the file from the Res\raw directory to
Directory of SD cards (/sdcard/dictionary)
if (! ( New File (DatabaseFileName)). Exists ())
{
Get the InputStream object that encapsulates the Dictionary.db file
InputStream is = Getresources (). Openrawresource (R.raw.dictionary);
FileOutputStream fos = new FileOutputStream (databasefilename);
byte[] buffer = new byte[8192];
int count = 0;
Start copying dictionary.db files
while ((count = is.read (buffer)) > 0)
{
Fos.write (buffer, 0, count);
}
Fos.close ();
Is.close ();
}
Open the Dictionary.db file in the/sdcard/dictionary directory
Sqlitedatabase Database = Sqlitedatabase.openorcreatedatabase (
DatabaseFileName, NULL);
return database;
}
catch (Exception e)
{
}
return null;
}
Several constants are used in the OpenDatabase method, which are defined in the main class (main) of the program, and the code is as follows:
Code
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";
}
How to publish a SQLite database (dictionary.db file) with the APK file