0

So the files that I'm creating are working fine and showing up exactly how they should on an emulator, I just can't get them to export anywhere where I can see them on my actual device. These files need to be accessible outside the application but I can't seem to make that happen.

This is how I declare the file variable

File FeedingReport = new File(Environment.DIRECTORY_ALARMS, "Feeding_" + DateFormat.format(date) + ".csv");

And then writing

FeedingReport.createNewFile();
FileOutputStream writer = openFileOutput(FeedingReport.getName(), MODE_WORLD_READABLE);

Like I said, the writing works fine as everything works perfectly on an emulator. I've set my permissions to read & write internal and external storage and as you can see I'm trying loads of different directories that I can see on my actual phone (eg. ALARMS) but the file still isn't showing up there.

Whatever I try nothing seems to let me view this file, please help!

DrewD
  • 1
  • 2
  • 1
    "but the file still isn't showing up there" -- how are you looking for the file? Did you call `sync()` on the `FileOutputStream` before closing it? Did you use `MediaScannerConnection` and `scanFile()` to have `MediaStore`'s index include this new file? And why do you think that using `MODE_WORLD_READABLE` is a good idea? – CommonsWare Feb 16 '15 at 19:07
  • I'm running my application and then physically going into the My Files app on my phone to look for it after it's been generated. Why is `MODE_WORLD_READABLE` not a good idea? And no I didn't do any of those things, I haven't come across those methods before. – DrewD Feb 16 '15 at 19:13
  • http://stackoverflow.com/a/28499732/115145 – CommonsWare Feb 16 '15 at 19:18
  • Strange that I hadn't heard of them in the tutorials I was looking at. Thanks! – DrewD Feb 16 '15 at 19:22
  • With regards to `MODE_WORLD_READABLE`, allowing unfettered access to your files is generally considered poor form, and not just on Android. It's to the point where `MODE_WORLD_READABLE` is deprecated IIRC. Use `MODE_PRIVATE`, and if you need to share the file, use something like `FileProvider`. – CommonsWare Feb 16 '15 at 19:25

1 Answers1

0

Try to inform the system about new files and directories which are created by you:

MediaScannerConnection.scanFile(MainActivity.this, new String[] {filePathWrite.toString()}, null, null);

//filePathWrite is Environment.DIRECTORY_ALARMS, 
//"Feeding_" + DateFormat.format(date) + ".csv" in your case

I had the same problem but answer in here solved my problem.

Victor
  • 2,450
  • 2
  • 23
  • 54
Nothingbutageek
  • 357
  • 1
  • 11