Article source: www.oschina.net/code/snippe…
Recommended article: blog.csdn.net/yegongheng/…
Sometimes our software uses SQLite database, this time how to package a completed database into our APK, actually very simple, is to put our database file in our phone, so there is no need to limit the place to write this code, in the first time to create the database can be, I think it works better in the software startup page. First of all, we should put the pre-written database files such as test.db into the RAW folder in the RES folder, and also into assets folder, because these two folders will not be compressed into binary format during the generation of APK, but remain unchanged.
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”; / / database name private static String DATABASE_PATH = “/ data/data/com. Android. Qufu. The 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{// if(dbExist){}else{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){}}}} /** * check whether the database exists * @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 ())// Check whether the folder exists. If it does not, create a 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 = “ht.www.sogou.com/websearch/f…” 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;” > \