0

I need some functionality where I can save a text file and an image in my device, that can somehow be accessed without the application.

For the text file I would just like to write my database in it. I need for the user to be able to later access that file. Is this possible? If so, how can I do it? As of now, I followed this tutorial, which sends me the message that is working. The issue is, cannot find the file. Where would it be? Or am I just missing something?

Here is my code for writing the file (which was as the tutorial suggested):

     try {
                File sdCard = Environment.getExternalStorageDirectory();
                File dir = new File (sdCard.getAbsolutePath() + "/documents");
                dir.mkdirs();
                File myFile = new File(dir, "mysdfile.txt");
                myFile.createNewFile();
                FileOutputStream fOut = new FileOutputStream(myFile);
                OutputStreamWriter myOutWriter = 
                                        new OutputStreamWriter(fOut);
                myOutWriter.append("I am writing something on the file as a test!");
                myOutWriter.close();
                fOut.close();
                Toast.makeText(getBaseContext(),
                        "Done writing SD 'mysdfile.txt'",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getBaseContext(), e.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }

Thanks!

Edit: just to add a couple more details, the directory I am getting from the

    Environment.getExternalStorageDirectory();

is /storage/emulated/0 is this what is expected? Or is there something wrong that I may be missing?

theJuls
  • 6,788
  • 14
  • 73
  • 160
  • http://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder – Salem May 21 '14 at 20:52
  • @Salem I saw that thread, but the solutions presented didn't work for me. Or they did, but I still have no idea how to access the directory in which the file is saved to. – theJuls May 21 '14 at 21:09

1 Answers1

0

Figured it out (well sortav...) Turns out it was the good and old "restart your device"

Source from where I found the solution: here

Community
  • 1
  • 1
theJuls
  • 6,788
  • 14
  • 73
  • 160