放在数据库目录下啊
public final static String DB = Environment.getDataDirectory().getPath() + "/data/" + getPackageName() + "/databases/";public class SeiDbOpenHelper extends SQLiteOpenHelper { public final static String DB_NAME = "sei.db";
public final static int DB_VERSION = 2; public SeiDbOpenHelper(){
super(MainApplication.getContext(),DB_NAME,null,DB_VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
createDb(db);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
} private static boolean checkIsDbExist(){
String fileName = Config.SystemFolder.DB + DB_NAME;
File file = new File(fileName);
return file.exists();
} private static void createDb(SQLiteDatabase db){
InputStream stream = null;
int size = 0;
try {
stream = MainApplication.getContext().getResources().getAssets().open("sei.db");
size = stream.available();
} catch (IOException e) {
Log.e("",e.getMessage());
e.printStackTrace();
}
if(size > 0){
File dir = new File(Config.SystemFolder.DB);
if(!dir.exists())
dir.mkdir();
String dbPath = Config.SystemFolder.DB + DB_NAME;
File file = new File(dbPath); //替换原有数据库,系统好像会自动生成一个!
if(file.exists()){
if(file.length() > 24*1024){
//非空数据库,备份
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String backdbName = "autoback_c_v" + String.valueOf(DB_VERSION) + "_" + formatter.format(new Date()) + ".db";
backDb(null,backdbName,DB_VERSION);
}
file.delete();
} if(!file.exists())
{
try {
FileOutputStream writer = new FileOutputStream(dbPath);
FileHelper.CopyFile(stream,writer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if(stream != null)
{
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public final static String DB = Environment.getDataDirectory().getPath() + "/data/" + getPackageName() + "/databases/";public class SeiDbOpenHelper extends SQLiteOpenHelper { public final static String DB_NAME = "sei.db";
public final static int DB_VERSION = 2; public SeiDbOpenHelper(){
super(MainApplication.getContext(),DB_NAME,null,DB_VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
createDb(db);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
} private static boolean checkIsDbExist(){
String fileName = Config.SystemFolder.DB + DB_NAME;
File file = new File(fileName);
return file.exists();
} private static void createDb(SQLiteDatabase db){
InputStream stream = null;
int size = 0;
try {
stream = MainApplication.getContext().getResources().getAssets().open("sei.db");
size = stream.available();
} catch (IOException e) {
Log.e("",e.getMessage());
e.printStackTrace();
}
if(size > 0){
File dir = new File(Config.SystemFolder.DB);
if(!dir.exists())
dir.mkdir();
String dbPath = Config.SystemFolder.DB + DB_NAME;
File file = new File(dbPath); //替换原有数据库,系统好像会自动生成一个!
if(file.exists()){
if(file.length() > 24*1024){
//非空数据库,备份
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String backdbName = "autoback_c_v" + String.valueOf(DB_VERSION) + "_" + formatter.format(new Date()) + ".db";
backDb(null,backdbName,DB_VERSION);
}
file.delete();
} if(!file.exists())
{
try {
FileOutputStream writer = new FileOutputStream(dbPath);
FileHelper.CopyFile(stream,writer);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if(stream != null)
{
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果数据库文件很大,可以采用切割文件,复制完成后重新合并文件。 Google “android 附加 数据库”,有解决方案