Last active
May 26, 2018 08:42
-
-
Save mostasim/be3e4c86492d64f7cdeb7a09c332ffe2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseContext extends ContextWrapper { | |
private static final String DEBUG_CONTEXT = "DatabaseContext"; | |
public DatabaseContext(Context base) { | |
super(base); | |
} | |
@Override | |
public File getDatabasePath(String name) { | |
File sdcard = Environment.getExternalStorageDirectory(); | |
String dbfile = sdcard.getAbsolutePath() + File.separator+ "database" + File.separator + name; | |
if (!dbfile.endsWith(".db")) { | |
dbfile += ".db" ; | |
} | |
File result = new File(dbfile); | |
if (!result.getParentFile().exists()) { | |
result.getParentFile().mkdirs(); | |
} | |
if (Log.isLoggable(DEBUG_CONTEXT, Log.WARN)) { | |
Log.w(DEBUG_CONTEXT, "getDatabasePath(" + name + ") = " + result.getAbsolutePath()); | |
} | |
return result; | |
} | |
/* this version is called for android devices >= api-11. thank to @damccull for fixing this. */ | |
@Override | |
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) { | |
return openOrCreateDatabase(name,mode, factory); | |
} | |
/* this version is called for android devices < api-11 */ | |
@Override | |
public SQLiteDatabase openOrCreateDatabase(String name, int mode, SQLiteDatabase.CursorFactory factory) { | |
SQLiteDatabase result = SQLiteDatabase.openOrCreateDatabase(getDatabasePath(name), null); | |
if (Log.isLoggable(DEBUG_CONTEXT, Log.WARN)) { | |
Log.w(DEBUG_CONTEXT, "openOrCreateDatabase(" + name + ",,) = " + result.getPath()); | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment