Can't Copy Sqlite Database From Assets
I try to copy SQLite database from assets directory to access it later. But I fail to do it! public class DatabaseAdapter { private static String DB_PATH = '/data/data/com.mypa
Solution 1:
I use this Helper and works fine:
publicclassDBHelperextendsSQLiteOpenHelper{
privatefinalstaticStringDB_PATH="/data/data/[YOUR PACKAGE HERE]/databases/";
String dbName;
Context context;
File dbFile;
publicDBHelper(Context context, String dbName, CursorFactory factory,
int version) {
super(context, dbName, factory, version);
this.context = context;
this.dbName = dbName;
dbFile= newFile(DB_PATH + dbName);
}
@Overridepublicsynchronized SQLiteDatabase getWritableDatabase() {
if(!dbFile.exists()){
SQLiteDatabasedb=super.getWritableDatabase();
copyDataBase(db.getPath());
}
returnsuper.getWritableDatabase();
}
@Overridepublicsynchronized SQLiteDatabase getReadableDatabase() {
if(!dbFile.exists()){
SQLiteDatabasedb=super.getReadableDatabase();
copyDataBase(db.getPath());
}
returnsuper.getReadableDatabase();
}
@OverridepublicvoidonCreate(SQLiteDatabase db) {}
@OverridepublicvoidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
privatevoidcopyDataBase(String dbPath){
try{
InputStreamassestDB= context.getAssets().open("databases/"+dbName);
OutputStreamappDB=newFileOutputStream(dbPath,false);
byte[] buffer = newbyte[1024];
int length;
while ((length = assestDB.read(buffer)) > 0) {
appDB.write(buffer, 0, length);
}
appDB.flush();
appDB.close();
assestDB.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
Take into account that the file extension of a database is .db and that my databases are into assets/databases/
Solution 2:
I have the same problem and I have fixed it with another approach. At the beginning, I declared the database path as everyone did:
dbPath="data/data/<my package name>/databases/data.db"
This is an exactly path, no mistake. But It' always fail when I try to open the OutPutFileStream to copy database. I don't know why. And then, I change the way to open the database as below:
dbPath = context.getDatabasePath(dbName);
OutputStreammyOutput=newFileOutputStream(dbPath.getAbsolutePath());
The problem has ben solved. So surprise.
Hope this helps.
Solution 3:
publicstaticvoidcopyDatabase(final Context ctx, String dbName) {
if (ctx != null) {
File f = ctx.getDatabasePath(dbName);
if (!f.exists()) {
// check databases existsif (!f.getParentFile().exists())
f.getParentFile().mkdir();
try {
InputStream in = ctx.getAssets().open(dbName);
OutputStream out = new FileOutputStream(f.getAbsolutePath());
byte[] buffer = newbyte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
Logger.i("Database copy successed! " + f.getPath());
} catch (Exception ex) {
Logger.e(ex);
}
}
}
}
Solution 4:
Please check the databases folder before your OutputStream.
like this,
FiledatabaseFile=newFile(context.getFilesDir().getAbsolutePath()
.replace("files", "databases"));
// check if databases folder exists, if not create it.if (!databaseFile.exists()){
databaseFile.mkdir();
}
Post a Comment for "Can't Copy Sqlite Database From Assets"