Database storage and retrieval
I want to store my database file in drawable folder and then create its back up in SD-Card. While trying to do this i am findind problem in what path to be given for drawable folder. I am doing this:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
//db = openOrCreateDatabase("demodatabase.sqlite",SQLiteDatabase.CREATE_IF_NECESSARY,null);
currentDBPath = "//drawable//demodatabase.sqlite";
db=openOrCreateDatabase(uri.toString(), 0, null);
System.out.println("====="+ currentDBPath);
backupDBPath = "{demodatabase.sqlite}";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} }catch (Exception e) {}
开发者_如何学JAVA
Thanx in advance.
you would put such files not in the drawable folder but in your assets folder and you can get an input stream to these files just by using
context.getAssets().open("demodatabase.sqlite")
精彩评论