0

I have created a simple application to take some user data and write it to a text file which gets saved on the external storage of my device. However, I am unable to access those files using my computer until after I have rebooted my device. Can anyone tell me why this is and if there is something I can do to fix it?

Here is the code I use to write data.

private void commitToFile(String worldOrApp, String xPos, String yPos, String orient) {
    Intent intent = getIntent();
    String filename = intent.getStringExtra(MainActivity.FILENAME) + ".txt";
    final String position = worldOrApp + " - x: " + xPos + "; y: " + yPos + "; alpha: " + orient + "\r\n";
    File myPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File myFolder = new File(myPath.getAbsolutePath()+"/test_folder");
    if (!myFolder.exists()) {
        myFolder.mkdirs();
    }
    File myFile = new File(myFolder, filename);
    try {
    FileOutputStream fileOutputStream = new FileOutputStream(myFile, true);
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
        outputStreamWriter.write(position);
        outputStreamWriter.flush();
        outputStreamWriter.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
rstemme
  • 13
  • 4

1 Answers1

0

Thanks to @CommonsWare for the direction. I found the following code at Android saving file to external storage

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this, new String[] { file.toString() }, null,
        new MediaScannerConnection.OnScanCompletedListener() {
            public void onScanCompleted(String path, Uri uri) {
            }
        });

which I placed directly underneath the exception catch in my code, and updated file to myFile, which is the relevant File for my commitToFile method.

Community
  • 1
  • 1
rstemme
  • 13
  • 4