0

I want to create a textfile on my sdcard. I found lots of codesamples and tried them. None of them responded any Exception so i expected them to work, but when I was exploring my sdcard, I could not find a textfile. Here is the attempt I am trying:

 try {
            File sdcard = Environment.getExternalStorageDirectory();

            File myFile = new File(sdcard,"text.txt");
            myFile.createNewFile();

            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter =
                    new OutputStreamWriter(fOut);
            myOutWriter.append(encodedString); // this is a long string
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(),
                    "written to: "+myFile.getAbsolutePath(),
                    Toast.LENGTH_SHORT).show();

Here is what the Toast is telling me: What I expect: "written to: /sdcard/text.txt" What I get: "written to: /storage/emulated/0/text.txt"

Why is the app not saving the textfile as intended onto the sdcard and what do I have to change in order to make it do it?

MojioMS
  • 1,583
  • 4
  • 17
  • 42

1 Answers1

0

getExternalStorageDirectory() does not necessary return /sdcard directory. See here Environment.getExternalStorageDirectory does not return the path to the removable storage

Community
  • 1
  • 1
Zamrony P. Juhara
  • 5,222
  • 2
  • 24
  • 40
  • I think my problem is here, okay. Can u add what I have to change in the code in order to have the file saved on the sdcard? – MojioMS Feb 01 '16 at 01:58
  • I haven't try but I think /proc/mounts shell command will returns list of mounted storages. Process procmount = Runtime.getRuntime.exec("/proc/mounts"); and parse data returned by Process.getOutputStream() – Zamrony P. Juhara Feb 01 '16 at 03:09