0

When I create a file in my app on the SD card it is visible in a file manager like ES Explorer.

However, it is not visible when the device is connected to the computer until the file is renamed in the file manager. Then it promptly becomes visible. This happens on a Nexus 4. Is there an error in my code or some other quirk I'm experiencing?

// Mostly from http://stackoverflow.com/a/2661882/572635
public void exportDatabaseFile() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        String dbPath = "/data/my.app/databases/db.db";
        FileChannel src = new FileInputStream(new File(data, dbPath)).getChannel();
        FileChannel dest = new FileOutputStream(new File(sd, "db.db")).getChannel();
        dest.transferFrom(src, 0, src.size());
        src.close();
        dest.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
dtech
  • 13,741
  • 11
  • 48
  • 73
  • It could be due to the new way storage gets virtualized on 4.x for potentially having multiple accounts. Can you see a folder called emulated/0/ somewhere in your sdcard. I don't remember the exact path. – Stephan Branczyk May 19 '14 at 21:08
  • Use `MediaScannerConnection` and `scanFile()` to index the file. The MTP connection only shows indexed files. – CommonsWare May 19 '14 at 21:15
  • @CommonsWare thanks! I'll try that and if it works and you make it into an answer I'll accept it – dtech May 19 '14 at 21:16

0 Answers0