3

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

Chuck Finley
  • 295
  • 1
  • 4
  • 12
  • have you tried printing the absolute path (http://developer.android.com/reference/java/io/File.html#getAbsolutePath()) from your `file` variable and then tried navigating there using `adb shell`? – Daniel Smith Jan 29 '13 at 22:20
  • 1
    Did you add to your manifest file? – ottel142 Jan 29 '13 at 22:21
  • The permission is set. It was throwing an error before I had it, so I'm sure that part is good. It just doesn't seem to be creating the file anywhere. – Chuck Finley Jan 29 '13 at 22:23
  • how about that absolute path? – Daniel Smith Jan 29 '13 at 22:25
  • It's returning /storage/emulated/0/Download/_Ascent_Test.txt but I'm unable to access that location from a computer. I need to be able to grab the file and open it on a regular machine, so I need to save it somewhere that I have permissions to grab it from. – Chuck Finley Jan 29 '13 at 22:28
  • 1
    You can use `adb pull /mnt/shell/emulated/0/Download/_Ascent_Test.txt` to get to the file. Or use the file view in DDMS in Eclipse. – Wolfram Rittmeyer Jan 31 '13 at 13:38

3 Answers3

5

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.

Wolfram Rittmeyer
  • 2,402
  • 1
  • 18
  • 21
1

I wanted to do a very similar thing. Here's how I got it to work using your code EXACTLY.

After running my code to make the file(/storage/emulated/0/Download/_Ascent_Test.txt):

I downloaded the ES File Explorer.

In the "Fast Access" menu towards the bottom I turned "Show hidden files" ON.

Then, also in the "Fast Access" menu, go to local -> / Device.

Now you will be able to navigate to the /storage folder and all the way down to _Ascent_test.txt

From there you can open it and email it yourself. Hope this helped!

-1

So this seems to be a known issue with Nexus 4 and 7. I still don't have a workaround, but for now, using Astro File Manager to email myself will solve the immediate issue at hand.

Saving files on external storage on Nexus 7 and retrieving from PC

Nexus 4 not showing files via MTP

Always fun to waste several hours on something like this. Gah!

Community
  • 1
  • 1
Chuck Finley
  • 295
  • 1
  • 4
  • 12