0

Can't write my string to file on external storage. I have write and read external storage permissions in Manifest. Also added this one

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

from previous similar questions. But nothing seems to work. Thanks in advance

  if (isSDCARDAvailable()) {
        writeToFile(resultJson.toString());
  }else {
        Log.d(TAG, "false");
  }    


 private void writeToFile(String string){
            String filename = "routes.txt";

            File sdcard = Environment.getExternalStorageDirectory();
            File file = new File(sdcard.getPath(), filename);
            try {
                FileWriter writer = new FileWriter(file);
                writer.write(string);
                writer.flush();
                writer.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public static boolean isSDCARDAvailable(){
            return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        }
Zhangali Bidaibekov
  • 591
  • 1
  • 7
  • 13
  • You don't need to mount or unmount external file systems. You need the permission to **access** and write to the external file system. Also, do you get any error? – UeliDeSchwert Aug 18 '16 at 09:16
  • This question has been answered already - http://stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android – pVas94 Aug 18 '16 at 09:19
  • Yeah, I also thought so, just saw it in here: http://stackoverflow.com/a/11963713/3847464. No I don't get any error. When debugging also didn't see anything noticeable, every line is executed. But no files on sdcard whatsoever – Zhangali Bidaibekov Aug 18 '16 at 09:20
  • @pVas94, answer provided there also doesn't work – Zhangali Bidaibekov Aug 18 '16 at 09:25
  • Possible duplicate of [Can't write to external storage on Android](http://stackoverflow.com/questions/11963428/cant-write-to-external-storage-on-android) – Tom Lord Aug 18 '16 at 09:32

2 Answers2

0

Please use this permission instead of mount and unmount, it will work. If it does not, please paste the error you are getting.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Amit Bhandari
  • 3,014
  • 6
  • 15
  • 33
0

If every lane is executed, Log the string to see if it's empty ?

pVas94
  • 31
  • 3