可以将 dictionary.db 文件复制到 Eclipse Android 工程中的 resaw 目录中,如图 1 所示。所有在
resaw 目录中的文件不会被压缩,这样可以直接提取该目录中的文件。
使用 openDatabase 方法来打开数据库文件,如果该文件不存在,系统会自动创建/sdcard/dictionar
y 目录,并将 resaw 目录中的 dictionary.db 文件复制到/sdcard/dictionary 目录中。openDatabase
方法的实现代码如下:
代码
private SQLiteDatabase openDatabase()
{
try
{
// 获得 dictionary.db 文件的绝对路径
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/dictionary 目录中存在,创建这个目录
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/dictionary 目录中不存在
// dictionary.db 文件,则从 resaw 目录中复制这个文件到
// SD 卡的目录(/sdcard/dictionary)
if (!(new File(databaseFilename)).exists())
{
// 获得封装 dictionary.db 文件的 InputStream 对象
InputStream is = getResources().openRawResource(R.raw.dictionary);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[8192];
int count = 0;
// 开始复制 dictionary.db 文件
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
// 打开/sdcard/dictionary 目录中的 dictionary.db 文件
SQLiteDatabase database = SQLiteDatabase.openOrCreateDatabase(
databaseFilename, null);
return database;
}
catch (Exception e)
{
评论2
最新资源