Trying to save SQLite db to sdcard [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Moving my db to sd card not working
I'm trying to save a sqlite file to my sdcard. I'm using the code from this question
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "\\data\\com.test.mytest\\databases\\test_db";
String backupDBPath = "test_db";
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) {
}
This is a very highly rated answer, so I would think it should work pretty simply. I get no errors in logcat. I don't see any created directory/created files. I also have the "write to external" permission in my manifest.

File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "/data/packagename/databases/DATABASENAME";
String backupDBPath = "/backup/"+"main.db";
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();
} else {
Log.e(TAG, "File does not exist: " + currentDBPath);
}

Don t forget to declare permission in amnifest file
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Related

Restore and delete the old SQLite database with new SQLite database in android

I am developing an app where I have to backup and restore my database. I have wrote the code for backup and it's working but I want to restore the backup database in my app and delete the one which is already present there. Please provide the solution for non rooted android devices.
Thank you!
My backup code:
private void exportDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+getPackageName()+"/databases/"+DatabaseHelper.DATABASE_NAME;
String backupDBPath = "abcrecord.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch(IOException e) {
e.printStackTrace();
}
}
Your code shows that you can backup your database. So for restoring this database we switch the source and destination files as below:
private void RestoreDB(){
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
FileChannel source=null;
FileChannel destination=null;
String currentDBPath = "/data/"+getPackageName()+"/databases/"+DatabaseHelper.DATABASE_NAME;
String backupDBPath = "abcrecord.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
try {
source = new FileInputStream(backupDB).getChannel();
destination = new FileOutputStream(currentDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(this, "DB restored!", Toast.LENGTH_LONG).show();
backupDB.delete(); //for deleting the backup database after restoring
} catch(IOException e) {
e.printStackTrace();
}
}

How To Copy File From SD to Local Storage on Android

I know with Kit Kat you can only write to your applications package specific directory on SD Cards. I was however under the impression you could still copy files from an SD card to local storage with the:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
I am simply testing if I can copy one file. If I am able to do that I will add the code to search the entire SD card DCIM folder. For now I have the following code (please forgive the messiness of the code, I have written C# and vb.net but java is still very new to me):
String dirPath = getFilesDir().getAbsolutePath() + File.separator + "TCM";
File projDir = new File(dirPath);
if (!projDir.exists())
projDir.mkdirs();
String CamPath = projDir + File.separator + tv2.getText();
File projDir2 = new File(CamPath);
if (!projDir2.exists())
projDir2.mkdirs();
File LocalBuck = new File(projDir2 + File.separator );
String imageInSD = Environment.getExternalStorageDirectory().getAbsolutePath();
File directory1 = new File (sdCard.getAbsolutePath() + "/DCIM");
File directory = new File(directory1 + "/100SDCIM");
File Buckfile = new File(directory, "/BigBuck.jpg");
try {
exportFile(Buckfile, LocalBuck);
} catch (IOException e) {
e.printStackTrace();
}
Here is my code for the export function/application:
private File exportFile(File src, File dst) throws IOException {
//if folder does not exist
if (!dst.exists()) {
if (!dst.mkdir()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyy_MM_dd_HHmmss").format(new Date());
File expFile = new File(dst.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//Straight to Error Handler
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(expFile).getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
return expFile;
}
Here is what my emulator looks like:
Looking for: Debug of SD Location
Should Find It?: EmulatorShowingSd
Question: Am I even able to copy a file from the SD card to local storage after KitKat; if so what is wrong in the code causing the exception to be thrown when it tries to access the SD card file?

Merging two databases in Android

In my app I have implemented options to backup and restore the database to a SDcard. When I restore the database, I must have the currently saved data included with the backed up data. How to make this possible?
This is how I am restoring database.
Restore:
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath= "//data//" + "PackageName"
+ "//databases//" + "DatabaseName";
String backupDBPath = "/BackupFolder/DatabaseName";
File backupDB= new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dest = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getBaseContext(), backupDB.toString(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG)
.show();
}
Try to attach backed to your current db in this way:
sqlite> ATTACH DATABASE 'testDB1.db' as 'DB1';
sqlite> ATTACH DATABASE 'testDB2.db' as 'DB2';
sqlite> ATTACH DATABASE 'testDB3.db' as 'DB3';
SQLite - ATTACH Database

Save File in Music Directory on Sd Card Android

How do i save a .mp3 file to the Music directory on the sdCard.
The code below always saves the file to the downloads folder without any extensions
private FileOutputStream getOutStream(String fileName) throws FileNotFoundException{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
String sdpath = Environment.getExternalStorageDirectory()
+ "/";
mSavePath = sdpath + "download";
File file = new File(mSavePath);
if (!file.exists()) {
file.mkdir();
}
File saveFile = new File(mSavePath, fileName);
return new FileOutputStream(saveFile);
}else{
mSavePath = mContext.getFilesDir().getPath();
return mContext.openFileOutput(fileName , Context.MODE_WORLD_READABLE);
}
}
mSavePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
File file = new File(mSavePath+"/filename.mp3");

is it possible backup and RESTORE a database file in android? non root devices [duplicate]

This question already has answers here:
Backup and restore SQLite database to sdcard
(3 answers)
Closed 8 years ago.
in my app I need get a backup of my database,
but after I'll need restore it again,
i have read somethings, but i do not sure if this is necessary to have a rooted device,
i need backup/restore the all data in non root devices, is it possible?
my first idea was creating a txt file for write the select, and later insert it again.
but i believe this is much "problem" then i don't know if this is possible copy the database and paste in sd card for backup, and copy from sd card and paste in path of database for restore for non root devices.
Here is some code to make it work
private void importDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<package name>"
+ "//databases//" + "<database name>";
String backupDBPath = "<backup db filename>"; // From SD directory.
File backupDB = new File(data, currentDBPath);
File currentDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(backupDB).getChannel();
FileChannel dst = new FileOutputStream(currentDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Import Successful!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
.show();
}
}
private void exportDB() {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//" + "<package name>"
+ "//databases//" + "<db name>";
String backupDBPath = "<destination>";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
Toast.makeText(getApplicationContext(), "Backup Successful!",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
.show();
}
}

Categories