Reputation: 306
How can I get the latest SQLite database (on the screenshot below, I want to get 11408102018000005) has been back up on SD Card if there is more than one .db
file?
Note:
The file type is dynamic. so the last number on the image is incrementing.
and this is my code when restoring a SQLite database.
private void restoreDB() {
try {
File[] sd = getContext().getExternalFilesDirs(null);
if (sd[1].canWrite()) {
File backupDB = new File(sd[1] + "/" + curControlNo); //<--- what to put here. This is just a test path and name
File currentDB = new File(getContext().getDatabasePath(DBHelper.DB_NAME).getPath());
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getContext(), "Import Successful!", Toast.LENGTH_SHORT).show();
openHome();
}
} catch (Throwable e) {
Toast.makeText(getContext(), "Import Failed!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
Upvotes: 2
Views: 163
Reputation: 56953
You can use the File's listFiles
method to get an array of the files in the directory, sort the array so the latest is first in the list and then use the first element of the array. e.g.
File[] dirlist = sd.listFiles(); //<<<<<<<< gets the list of files
Upvotes: 0
Reputation: 21756
You need to create file and pass DB name that you want to save like below:
File backupDB = new File(sd[1], DBHelper.DB_NAME);
Hope it will work.
Upvotes: 1