I want the TXT files (and the folder containing them) generated by my App to be accessible by my phone's file browser, or even from a PC when I plug the phone via USB. Android's official documentation describes the different types of storing depending if it is "internal" or "external" (Google's terminology). And of course, I don't want those TXT files to be deleted when I uninstall the App.
To avoid any confusions:
The fact that I want to hardcode the file location in the App, doesn't imply that I would use neccessarily the complete absolute filepath as a parameter. What i mean is the relative filepath will be hardcoded (same task, same file, same place).
...that said...
The app is for personal use (I mean to use it myself), so fulfilling Google Play Store's requirements is not a priority for now.
Code example (what I tried)
try {
// Dedicated folder would be better...
// ...but not even in the root folder works...
// FIXME T_T
FileWriter writer = new FileWriter("/storage/emulated/0/myfile.txt");
writer.write(my_string_for_file);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
// ...always throws FileNotFoundException with Permission DENIED
It would be Python on PC, stuff's done in 5 minutes!! 3 hours and I still can't figure it out?? I wanna cry...
So, I want to save some output always in the same file, in the same path. And I want that TXT file to be accessible from my PC when I plug it in via USB, as well as from my phone's file browser. In Python, for example, for a PC app, I would achieve this with 3 single lines of code! It can't be much harder in Android, right? (or so I hope)
I am aware that, for what I describe here, it is technically "documents and other kind of files", and what Google or Android Developers consider "external storage" (but what Android Users consider Internal Storage!). Well, Android Developers Documentation has a section for this in this link.
But the example's Code Snippet is overkill for my needs: I don't want the User to choose filepath: I want to hardcode the TXT files' paths into /storage/emulated/0/myapp/
directory. Instead, the example seems to provide some way to open some kind of "Save As" file dialog, which I don't want.
According to this article, it suggests me to use Environment.getExternalStoragePublicDirectory
, and if I look to this method's documentation it says this method is deprecated from API 28. I have been looking other docs about the subject, but I am still confused about how to proceed. Supposing that I already have the text I want to write into a String
variable:
How would I make sure that files would be written to some dedicated folder like
/storage/emulated/0/myapp
or/storage/emulated/0/myfolder
instead of the root folder/storage/emulated/0
? WillgetExternalFilesDir()
return/storage/emulated/0
or is there a better method?Which is the simplest code to just write the file containing the
String
variable's text with a hardcoded filename into the dedicated directory, but it will still persist after desinstalling the app?
If it is either /storage/emulated/0
or /storage/emulated/0/Documents
, any of both is fine by me. The most important thing is that I can access those files from outside my App that is gonna make them and, if possible, that uninstalling the mentioned App won't delete them.