就是先将数据库存到raw上,然后在第一次安装的时候复制到sdcard上,以后就直接打开数据库了。
一下是代码:
private Context context;
private String rootDirectory = "/data/data/CrazyNovelV1_0.com/data/";
private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/crazynovel";
private final String DATABASE_FILENAME = "crazynovel.db";
//复制小于1M的数据库程序
public 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文件,则从res\raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
if (!(new File(databaseFilename)).exists())
{
// 获得封装dictionary.db文件的InputStream对象
InputStream is = context.getResources().openRawResource(R.raw.crazynovel);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[7168];
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)
{
}
}
小于1M的可以用上面的方法复制,但是如果文件大于1M则不行,需要分割后再合并,最终拷贝到sdcard上。
以下是解决方案:
private Context context;
private String rootDirectory = "/data/data/CrazyNovelV1_0.com/data/";
private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/crazynovel";
private final String DATABASE_FILENAME = "crazynovel.db";
//复制小于1M的数据库程序
public 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文件,则从res\raw目录中复制这个文件到
// SD卡的目录(/sdcard/dictionary)
if (!(new File(databaseFilename)).exists())
{
//复制文件
copydb(R.raw.crazynovel1,DATABASE_PATH+ "/crazynovel1",944128);
copydb(R.raw.crazynovel2,DATABASE_PATH+ "/crazynovel2",944128);
copydb(R.raw.crazynovel3,DATABASE_PATH+ "/crazynovel3",944128);
copydb(R.raw.crazynovel4,DATABASE_PATH+ "/crazynovel4",944128);
copydb(R.raw.crazynovel5,DATABASE_PATH+ "/crazynovel5",944128);
copydb(R.raw.crazynovel6,DATABASE_PATH+ "/crazynovel6",944128);
copydb(R.raw.crazynovel7,DATABASE_PATH+ "/crazynovel7",944128);
copydb(R.raw.crazynovel8,DATABASE_PATH+ "/crazynovel8",944128);
copydb(R.raw.crazynovel9,DATABASE_PATH+ "/crazynovel9",944128);
copydb(R.raw.crazynovel10,DATABASE_PATH+ "/crazynovel10",944128);
copydb(R.raw.crazynovel11,DATABASE_PATH+ "/crazynovel11",944128);
copydb(R.raw.crazynovel12,DATABASE_PATH+ "/crazynovel12",669696);
//合并文件
File[] files = new File[12];
files[0] = new File(DATABASE_PATH+ "/crazynovel1");
files[1] = new File(DATABASE_PATH+ "/crazynovel2");
files[2] = new File(DATABASE_PATH+ "/crazynovel3");
files[3] = new File(DATABASE_PATH+ "/crazynovel4");
files[4] = new File(DATABASE_PATH+ "/crazynovel5");
files[5] = new File(DATABASE_PATH+ "/crazynovel6");
files[6] = new File(DATABASE_PATH+ "/crazynovel7");
files[7] = new File(DATABASE_PATH+ "/crazynovel8");
files[8] = new File(DATABASE_PATH+ "/crazynovel9");
files[9] = new File(DATABASE_PATH+ "/crazynovel10");
files[10] = new File(DATABASE_PATH+ "/crazynovel11");
files[11] = new File(DATABASE_PATH+ "/crazynovel12");
FileOutputStream fos = new FileOutputStream(databaseFilename);
CreateFromRawDbFiles(files,fos); }
catch (Exception e)
{
}
}
//复制文件
private void copydb(int dbint,String databaseFilename,int FileSize)
{
try
{
// 获得封装dictionary.db文件的InputStream对象
InputStream is = context.getResources().openRawResource(dbint);
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = new byte[FileSize];
int count = 0;
// 开始复制dictionary.db文件
while ((count = is.read(buffer)) > 0)
{
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
catch(Exception e)
{}
}
//合并并拷贝数据
private void CreateFromRawDbFiles(File[] filelist,FileOutputStream Fos)
{
try
{
for (File file : filelist)
{
InputStream inputFile = new FileInputStream(file);
int TotalLength = 0;
try
{
TotalLength = inputFile.available();
}
catch (IOException e)
{
}
// Reading and writing the file Method 1 :
byte[] buffer = new byte[TotalLength];
int len = 0;
try
{
len = inputFile.read(buffer);
}
catch (IOException e)
{
}
Fos.write(buffer,0,len);
inputFile.close();
}
Fos.close();
}
catch(IOException e)
{}
}