The Android database packages the instance code published with APK

  • 2020-05-17 06:32:42
  • OfStack

Actually very simple, is to put our database files in our phone, so don't limit where to write the code, at the time of first create the database can be, I feel better in the software starting page 1 point, first of all, we should write in advance good database files such as test. db on res raw folder in the folder, can also be put into assets, because the two folder won't at the time of generating APK will not be compressed.
1. DataBaseUtil is used to transfer db file copy from raw to the phone. The code is as follows


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import com.ata.app.R;
/**
 * copy The database to apk package 
 * 
 * @author NGJ
 * 
 */
public class DataBaseUtil {
 private Context context;
 public static String dbName = "Kao.db";//  Database name 
 private static String DATABASE_PATH;//  The path of the database in the phone 
 public DataBaseUtil(Context context) {
  this.context = context;
  String packageName = context.getPackageName();
  DATABASE_PATH="/data/data/"+packageName+"/databases/";
 }
 /**
  *  Determine if the database exists 
  * 
  * @return false or true
  */
 public boolean checkDataBase() {
  SQLiteDatabase db = null;
  try {
   String databaseFilename = DATABASE_PATH + dbName;
   db = SQLiteDatabase.openDatabase(databaseFilename, null,SQLiteDatabase.OPEN_READONLY);
  } catch (SQLiteException e) {
  }
  if (db != null) {
   db.close();
  }
  return db != null ? true : false;
 }
 /**
  *  Copy the database to the specified folder on the phone 
  * 
  * @throws IOException
  */
 public void copyDataBase() throws IOException {
  String databaseFilenames = DATABASE_PATH + dbName;
  File dir = new File(DATABASE_PATH);
  if (!dir.exists())//  Determine if the folder exists, and if it does not, create a new folder 1 a 
   dir.mkdir();
  FileOutputStream os = new FileOutputStream(databaseFilenames);//  Get the write - in of the database file 
  InputStream is = context.getResources().openRawResource(R.raw.kao);//  Get the data flow of the database file 
  byte[] buffer = new byte[8192];
  int count = 0;
  while ((count = is.read(buffer)) > 0) {
   os.write(buffer, 0, count);
   os.flush();
  }
  is.close();
  os.close();
 }
}

2. Add the following method to the required activity for the specific copy operation

Java code   
privatevoid copyDataBaseToPhone() {  
        DataBaseUtil util = new DataBaseUtil(this);  
        //  Determine if the database exists   
        boolean dbExist = util.checkDataBase();  

        if (dbExist) {  
            Log.i("tag", "The database is exist.");  
        } else {//  It doesn't exist raw The database is written to the phone   
            try {  
               util.copyDataBase();  
           } catch (IOException e) {  
               thrownew Error("Error copying database");  
           }  
        }  
    }  

3. Check whether there is SDCard and execute copy. (the individual feels that it is possible not to detect the existence of SD card, but there seems to be a problem with not detecting the SD card. What is the reason for the procedure?)


boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
  if(hasSDCard){
   copyDataBaseToPhone();
  }else{
   showToast(" Not detected SDCard");
  }


Related articles: