I'm trying to run a simple database backup in my application, but the files are not showing up when I connect the device to my PC, but appears to be OK in the Android File Manager. The file is being copied to "Downloads" folder by the way...
Here is the code I'm running to copy it:
//...
private void backupDatabase(){
String downloadsPath = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath();
String backupDirectoryPath = downloadsPath+"/myapp_backups/";
File backupDirectory = new File(backupDirectoryPath);
backupDirectory.mkdirs();
String bkpFileName = "backup_"+(new Date().getTime())+".bkp";
String src = mContext.getDatabasePath(DatabaseHelper.DATABASE_NAME).getAbsolutePath();
String dest = backupDirectoryPath + bkpFileName;
FileUtil.copyFile(src, dest);
}
//...
And here what FileUtil.copyFile
function looks like:
public static boolean copyFile(String src, String dest){
boolean success;
try{
if(!isFile(src)){
throw new Exception("Source file doesn't exist: "+src);
}
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
success = true;
}catch (Exception exception){
exception.printStackTrace();
success = false;
}
return success;
}
The code works on both devices we tested but none shows the file on the PC.
What I'm missing?
adb shell
,cd mnt/sdcard/Download/
andls
. You can even use the terminal window inside AndroidStudio. That way you'll be reading directly from the file system. (adjust the directory appropriately). – LullyIntent
with the DB file attached to it. So it can be send directly to you. – Lully