Reputation: 6117
Here is my code:
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
String es= System.getenv("EXTERNAL_STORAGE");
if (sd.canWrite())
{
String currentDBPath = "/data/com.my.package/databases/my.db";
String backupDBPath = "my_db_backup.db3";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(es, 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();
}
Toast.makeText(getBaseContext(), backupDB.toString(), Toast.LENGTH_LONG).show();
}
}
catch (Exception e) {
Log.e("APP ERROR", "APP DB Backup", e);
}
All executes successfully . The EXTERNAL_STORAGE path is : storage/emulated/legacy . But when I use DDMS, the EXTERNAL_STORAGE path is there, but there is no file present. I have an HTC One...
UPDATE: 11-01-2013 I tried all suggestions below, no error is thrown though just like in my code, and leads me to believe that its MY phone thats just silently not allowing me to copy anything to my SD card. I have an HTC one...is there any setting or procedure I need to do with my phone prior to executing the "copy db to sd card" code?
Upvotes: 1
Views: 952
Reputation: 6852
First thing you need to check whether you have gave write permission for writing files to sdcard in manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For reference check this link
Thanks
Upvotes: 0
Reputation: 28484
Try this works like charm with me
public void exportDatabse(String databaseName) {
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "//data//"+getPackageName()+"//databases//"+databaseName+"";
String backupDBPath = "backupname.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) {
}
}
NOTE :
Just pass the name of database without specifying extension.
Like this :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
exportDatabse("my"); // Correct
exportDatabse("my.db"); // Wrong
}
Upvotes: 2
Reputation: 2707
can you try this one
void getDBBackUp(){
File cacheDir;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cacheDir = new File(android.os.Environment.getExternalStorageDirectory(),"NewFolder");
else
cacheDir = getCacheDir();
if (!cacheDir.exists())
cacheDir.mkdirs();
Calendar c = Calendar.getInstance();
String temp =""+c.get(Calendar.DATE) +"-"+ (c.get(Calendar.MONTH)+1) +"-"+ c.get(Calendar.YEAR) +"-"+ c.get(Calendar.HOUR)+"-"+ c.get(Calendar.MINUTE) +"-"+ c.get(Calendar.SECOND);
String PACKAGE_NAME = getApplicationContext().getPackageName();
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
File dir = new File (sd.getAbsolutePath() + "/NewFolder");
if (dir.canWrite()) {
String currentDBPath = "//data//"+PACKAGE_NAME+"//databases//DatabaseName";
String backupDBPath = "DB_"+temp;
File currentDB = new File(data, currentDBPath);
File backupDB = new File(dir, backupDBPath);
AppLog.logString(TAG+"PACKAGE_NAME : "+PACKAGE_NAME);
AppLog.logString(TAG+"currentDBPath : "+currentDBPath);
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();
}
AppLog.logString(TAG+"backupDB : "+backupDB);
ErrorDialog.dispDialog(LogInActivity.this, "Backup Store At: "+backupDB, "eLitePOS");
}
} catch (Exception e) {
e.printStackTrace();
}
}
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 0