Back to project page palhike.
The source code is released under:
GNU General Public License
If you think the Android project palhike listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.palhike.android; /*from w w w . j a va 2 s. c o m*/ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class DataBaseHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static String DB_PATH=""; private static final String DATABASE_NAME = "hike-new.db"; private Context myContext; private SQLiteDatabase mDataBase; private String getDbAbsolutePath(){ return DB_PATH + File.separator + DATABASE_NAME; } public DataBaseHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); this.myContext = context; if (android.os.Build.VERSION.SDK_INT >= 17) { DB_PATH = context.getApplicationInfo().dataDir + "/databases"; } else { DB_PATH = "/data/data/" + context.getPackageName() + "/databases"; } this.myContext = context; } public void createDataBase() throws Exception { //If database not exists copy it from the assets boolean mDataBaseExist = checkDataBase(); // Toast.makeText(myContext, "Db file: "+ (mDataBaseExist ? "exists": "does not exist"), Toast.LENGTH_LONG).show(); if(!mDataBaseExist) { Toast.makeText(myContext, "Creating new Db", Toast.LENGTH_LONG).show(); // try Writable... // this.getReadableDatabase(); // Toast.makeText(myContext, "got readable Db: ", Toast.LENGTH_LONG).show(); // this.close(); try { //Copy the database from assests copyDataBase(); Log.e("DBCreated", "createDatabase database created"); } catch (IOException mIOException) { throw new Error("ErrorCopyingDataBase"); } catch (Exception e) { throw e; } // Toast.makeText(myContext, "Db file created", Toast.LENGTH_LONG).show(); } } //Open the database, so we can query it public boolean openDataBase() throws SQLException { //Log.v("mPath", mPath); mDataBase = SQLiteDatabase.openDatabase(getDbAbsolutePath(), null, SQLiteDatabase.CREATE_IF_NECESSARY); //mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); return mDataBase != null; } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } private boolean checkDataBase() { File dbFile = new File(getDbAbsolutePath()); //Log.v("dbFile", dbFile + " "+ dbFile.exists()); return dbFile.exists(); } private void copyDataBase() throws Exception { File databaseFile = new File(DB_PATH); // check if target databases folder exists, if not create one and its // sub-folders if (!databaseFile.exists()) { if (!databaseFile.mkdir() ) { throw new Exception("Could not create directory: "+ databaseFile.getPath()); }; } String outFileName = getDbAbsolutePath(); String inFileName = "databases/" + DATABASE_NAME; try { InputStream in = (this.myContext).getAssets().open(inFileName); OutputStream out = new FileOutputStream(outFileName); // Transfer bytes from the sample input file to the sample // output file byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); // Close the streams out.close(); in.close(); } catch (IOException e) { throw e; } catch (Exception e) { throw e; } } }