Android: save file to downloads that can be viewed later

Chuck Finley picture Chuck Finley · Jan 29, 2013 · Viewed 12.5k times · Source

I have an app that needs to collect a bunch of data while connected to a stream. I need to save this data to a file that I can later pull off my device and analyze using a standard computer.

My code currently looks like:

private void saveData(byte[] data){
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File file = new File(path, "_Ascent_Test.txt");        
    try {                      
        FileOutputStream stream = new FileOutputStream(file, true);             
        stream.write(data);            
        stream.close();
        Log.i("saveData", "Data Saved");        
    } catch (IOException e) {
        Log.e("SAVE DATA", "Could not write file " + e.getMessage());
    }       
}

It's correctly hitting the "Data Saved" log without any errors yet I cannot find the file anywhere in the devices internal storage when I browse it from my computer.

What am I missing?

Thanks.

Edit: I'm needed to run this on a Nexus 7

Answer

Wolfram Rittmeyer picture Wolfram Rittmeyer · Jan 31, 2013

Files are not visible unless you make them explicitly available. See my blog post about the MediaScanner to read more about this.

It's the developer's job to take care of this and to make sure that all files, the user might want to access, are made available to the MediaScanner.