1

I've got a little Android app written for a friend (nothing complex, just extracts data from his phone into a CSV) that works fine, except the output file gets written to the directory returned by getExternalFilesDir(), which is rather deep in the file system for an average user to get hold of.

When I plug my phone into my laptop/PC via USB, it mounts as an external drive. Is there a way for my app to write to that directory?

Thanks

user1381745
  • 3,850
  • 2
  • 21
  • 35

1 Answers1

0

When I plug my phone into my laptop/PC via USB, it mounts as an external drive. Is there a way for my app to write to that directory?

Yes, insofar as that is Environment.getExternalStorageDirectory(). This is the equivalent of saying "I am going to write everything to the C: drive" on Windows. That was the dominant approach back in 1996, but we came to our senses and stopped cluttering up the root of a drive with files.

A middle ground option would be to put the file in one of the Environment.getExternalStoragePublicDirectory() locations, such as DIRECTORY_DOWNLOADS (if your minSdkVersion is 8 or higher) or DIRECTORY_DOCUMENTS (if your minSdkVersion is 19 or higher).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks - I trued the public directory (and DIRECTORY_DOCUMENTS, incidentally), but the file fails to write because the directory doesn't exist. Specifically, File.mkdirs() returns false. The app has the WRITE_EXTERNAL_STORAGE permission granted (confirmed in Settings). – user1381745 Nov 08 '16 at 13:34
  • @user1381745: Make sure that you are [handling runtime permissions](http://stackoverflow.com/questions/32635704/android-permission-doesnt-work-even-if-i-have-declared-it). – CommonsWare Nov 08 '16 at 13:40