Android database package with APK release

Source: Internet
Author: User

Sometimes our software used to SQLite database, this time how to pack a good database into our apk, in fact, is very simple, is to put our database files in our mobile phone, so do not have to limit where to write this code, in the first time to create a database can be, I think in the Software start Page effect is better, first we should put the pre-written database files such as test.db in the Res folder in the Raw folder, can also be put into the assets, because these two folders will not be generated when the APK is not compressed
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= "dinner.db";//Database name
private static String database_path= "/data/data/com.android.qufu.dinner/databases/";//the path of the database in the phone
int alpha = 255;
int b = 0;

public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.login);
Determine if the database exists
Boolean dbexist = Checkdatabase ();
if (dbexist) {

}else{//the database in raw to the phone
try{
Copydatabase ();
}catch (IOException e) {
throw new error ("Error copying database");
}
}

New Thread (New Runnable () {
public void Run () {
Initapp (); Initialization program

while (b < 2) {
try {
if (b = = 0) {
Thread.Sleep (20);
b = 1;
} else {
Thread.Sleep (50);
}
Updateapp ();
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}). Start ();


}
Start another activity, usually not
/* 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) {

}
}

}

}*/
/**
* Determine if 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;
}
/**
* Copy the database to the phone under the specified folder
* @throws IOException
*/
public void Copydatabase () throws ioexception{
String Databasefilenames =database_path+dbname;
File dir = new file (Database_path);
if (!dir.exists ())//Determines whether the folder exists, does not exist to create a new
Dir.mkdir ();
FileOutputStream OS = null;
try{
OS = new FileOutputStream (databasefilenames);//Get write stream of database file
}catch (FileNotFoundException e) {
E.printstacktrace ();
}
InputStream is = Loggin.this.getResources (). Openrawresource (r.raw.test);//Get the data stream of the database file
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 ();
}
}
/**
* Initialize, this is the start page is useless
*/
public void Initapp () {
}
}

Android database package with APK release

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.