private final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/db_exam"
private final String DATABASE_FILENAME = "tel.db"
public void extractDBFileFromRes(){
try {
String dbFileName = DATABASE_PATH + "/" + DATABASE_FILENAME
File dir = new File(DATABASE_PATH)
if (!dir.exists()){
dir.mkdir()
Log.i("SQLite", "dir made:" + DATABASE_PATH)
} else {
Log.i("SQLite", "dir exist:" + DATABASE_PATH)
}
try {
//如果数据库已经在SD卡的目录下存在,那么不需要重新创建,否则创建文件,并拷贝/res/raw下面的数据库文件
if (!(new File(dbFileName).exists())){
Log.i("SQLite", dbFileName + ":file not exist")
//res/raw数据库作为输出流
InputStream inputStream = this.getResources().openRawResource(R.raw.tel)
//测试
int size = inputStream.available()
Log.i("SQLite", "DATABASE_SIZE:" + 1)
Log.i("SQLite", "count:" + 0)
//用于存放数据库信息的数据流
FileOutputStream fileOutputStream = new FileOutputStream(dbFileName)
byte[] buffer = new byte[8192]
int count = 0
Log.i("SQLite", "count:" + count)
//把数据写入SD卡目录下
while ((count = inputStream.read(buffer)) >0 ) {
fileOutputStream.write(buffer, 0, count)
}
fileOutputStream.flush()
fileOutputStream.close()
inputStream.close()
}
} catch (FileNotFoundException e) {
Log.e("Database", "File not found")
e.printStackTrace()
}
} catch (IOException e) {
Log.e("Database", "IO exception")
e.printStackTrace()
}
}
public class DatabaseHelper extends SQLiteOpenHelper {/**
* 删除数据库
*
* @param context
* @return
*/
public boolean deleteDatabase(Context context) {
return context.deleteDatabase(name)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)