0

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?

CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51
  • https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android-so-that-they-are-visibl – CommonsWare Jun 21 '16 at 14:16
  • Thank you @CommonsWare, I will have a look – CarlosCarucce Jun 21 '16 at 14:16
  • Tried restarting the devices? – yardie Jun 21 '16 at 14:17
  • @andre3wap Yes. And both are using MTP mode – CarlosCarucce Jun 21 '16 at 14:18
  • the easier way to see if the issue is the MTP connection with PC or the code copying the file is to on terminal type `adb shell`, `cd mnt/sdcard/Download/` and `ls`. You can even use the terminal window inside AndroidStudio. That way you'll be reading directly from the file system. (adjust the directory appropriately). – Budius Jun 21 '16 at 14:33
  • @Budius thanks for your comment. It works, but I simply cant expect to a final user to know how to use ADB console =/ – CarlosCarucce Jun 21 '16 at 14:48
  • I agree with you. I commented that as a debug tool for you to identify the real issue. For a final user point of view I would suggest you to directly launch an e-mail `Intent` with the DB file attached to it. So it can be send directly to you. – Budius Jun 21 '16 at 14:58

1 Answers1

0

As pointed out in the question linked by @CommonsWare, I made a little research about MediaScannerConnection class and it solved the problem.

Here is the final code:

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);

//Scan the new file, so it will show up in the PC file explorer:
MediaScannerConnection.scanFile(
    mContext, new String[]{dest}, null, null
);
CarlosCarucce
  • 3,420
  • 1
  • 28
  • 51