有些时候我们的软件用到SQLite数据库,这个时候怎么把一个做好的数据库打包进我们的APK呢,其实很简单,就是把我们的数据库文件放到我们的手机里,所以不必局限在哪个地方写这个代码,在第一次创建数据库的时候可以,我觉得在软件起动页里效果更好一点,首先我们应该把事先写好的数据库文件比如test.db放到res文件夹里的raw文件夹里,也可以放到assets里,因为这两个文件夹不会在生成APK的时候被压缩成二进制格式,而是仍然保持原样。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import com.android.qufu.dinner.MealActivityGroup;
import com.android.qufu.dinner.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
public class Loggin extends Activity {
public static String dbName="test.db";//数据库的名字
private static String DATABASE_PATH="/data/data/com.android.qufu.dinner/databases/";//数据库在手机里的路径
int alpha = 255;
int b = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
//判断数据库是否存在
boolean dbExist = checkDataBase();
if(dbExist){
}else{//不存在就把raw里的数据库写入手机
try{
copyDataBase();
}catch(IOException e){
throw new Error("Error copying database");
}
}
new Thread(new Runnable() {
public void run() {
initApp(); //初始化程序
while (b < 2) {
try {
if (b == 0) {
Thread.sleep(20);
b = 1;
} else {
Thread.sleep(50);
}
updateApp();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
public void updateApp() {
alpha -= 5;
if (alpha <= 0) {
b = 2;
if(true){
try{
Intent in = new Intent(Loggin.this,MealActivityGroup.class);
Loggin.this.startActivity(in);
Loggin.this.finish();
}catch(Exception e){
}
}
}
}
/**
* 判断数据库是否存在
* @return false or true
*/
public boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String databaseFilename = DATABASE_PATH+dbName;
checkDB =SQLiteDatabase.openDatabase(databaseFilename, null,
SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB!=null){
checkDB.close();
}
return checkDB !=null?true:false;
}
/**
* 复制数据库到手机指定文件夹下
* @throws IOException
*/
public void copyDataBase() throws IOException{
String databaseFilenames =DATABASE_PATH+dbName;
File dir = new File(DATABASE_PATH);
if(!dir.exists())//判断文件夹是否存在,不存在就新建一个
dir.mkdir();
FileOutputStream os = null;
try{
os = new FileOutputStream(databaseFilenames);//得到数据库文件的写入流
}catch(FileNotFoundException e){
e.printStackTrace();
}
InputStream is = Loggin.this.getResources().openRawResource(R.raw.test);//得到数据库文件的数据流
byte[] buffer = new byte[8192];
int count = 0;
try{
while((count=is.read(buffer))>0){
os.write(buffer, 0, count);
os.flush();
}
}catch(IOException e){
}
try{
is.close();
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
?
class="sogou_sugg_feedbackquan" frameborder="0" scrolling="no" src="http://ht.www.sogou.com/websearch/features/yun4.jsp?pid=sogou-brse-596dedf4498e258e&w=1440&v=1425&st=1416038319115&od=265&ls=1416038293779&lc=1415936693878&lk=&sd=16&cd=0&kd=0&u=1414373472396828&y=148D13140B594345BE526B6FD91EC891&query=%5B%E4%BB%A3%E7%A0%81%E5%85%A8%E5%B1%8F%E6%9F%A5%E7%9C%8B%5D-Android%20%E6%95%B0%E6%8D%AE%E5%BA%93%E6%89%93%E5%8C%85%E9%9A%8FAPK%E5%8F%91%E5%B8%83|http%3A%2F%2Fwww.oschina.net%2Fcode%2Fpiece_full%3Fcode%3D9207&r=http%3A%2F%2Fwww.oschina.net%2Fcode%2Fsnippet_203635_9207" style="position: fixed; left: 0px; padding: 0px; margin: 0px; bottom: 0px; width: 1273px; height: 65px; font-family: Simsun;font-size:14px; background-color: transparent; border-style: none; display: block; z-index: 2147483645;">