0

I'm trying to write data into a text file on the sd-card. However, the result is, that the file is written in the internal storage (internal storage/myNewFolder). Where is my mistake in the code part? Here's the interesting part with the paths:

String state;
                state = Environment.getExternalStorageState();

                if (Environment.MEDIA_MOUNTED.equals(state))
                {
                File Root = Environment.getExternalStorageDirectory();
                File Dir = new File(Root.getAbsolutePath()+"/myNewFolder");
                if (!Dir.exists()) {
                    Dir.mkdir();
                }
                 File file = new File(Dir,"TrackLog.txt");

..........code skipped ........

               else
                {
                    Toast.makeText(getApplicationContext(),"SD not found",Toast.LENGTH_LONG).show();
                }

I skipped the rest of the code as it shouldn't be of interest for this problem.

edit: now changed to

                File Root = Environment.getExternalStorageDirectory();
                File Dir = new File(Root.getAbsolutePath()+"/myNewFolder");
                if (!Dir.exists()) {
                    Dir.mkdir();
                }
                 File file = new File(Dir,"TrackLog.txt");

So the file is simply written into myNewFolder in the internal storage - which is also fine with me.

ayanu99
  • 63
  • 8
  • https://stackoverflow.com/questions/8330276/write-a-file-in-external-storage-in-android Have you tried this? – Ruben Rudov Mar 06 '21 at 09:18
  • `I'm trying to write data into a text file on the sd-card.` Well your code is not trying that. You use Environment.getExternalStorageDirectory() which -look at the name- is called external storage at Android and has nothing to do with a removable micro SD card. That the file manager you are using calls that Internal storage well thats up to that file manager and makes the confusion complete. – blackapps Mar 06 '21 at 09:27
  • `if (!Dir.exists()) { Dir.mkdir(); }` change that to: `if (!Dir.exists()) { if ( ! Dir.mkdir()){ Toast(.. sorry could not create directory..); return;}; }` As now you try to write a file in a directory that does not even exist and you are not aware of that. – blackapps Mar 06 '21 at 09:30
  • Check this out: https://stackoverflow.com/questions/40068984/universal-way-to-write-to-external-sd-card-on-android – George Sepetadelis Mar 06 '21 at 10:03
  • @blackapps The file is written correctly and the folder is also created correctly but into the internal storage. So if I understand it correctly, I have to change ```File Root = Environment.getExternalStorageDirectory();``` into something else calling the mounted micro SD card. Or, if I want to continue writing into internal storage, the if condition in the beginning is obsolete ```if (Environment.MEDIA_MOUNTED.equals(state))``` and can be deleted - is that correct? – ayanu99 Mar 06 '21 at 19:22
  • getExternalStorageDirectory is always mounted as it is internal in the phone and not removable. If you want to write to removable micro sd card then have a look at second item returned by getExternalFilesDirs(null). – blackapps Mar 06 '21 at 19:39
  • Ok, thanks a alot - I have simply deleted the if condition and stick with the internal storage, as I am also fine with that. I simply didn't understand the thing with the external storage and the SD card - now I know it's not the same! – ayanu99 Mar 07 '21 at 10:39

0 Answers0