0

I want to create a text file in a folder in sdcard then write some data in that file. I check lots of pages like:

Saving to SD card as text file

https://stackoverflow.com/questions/9480584/reading-and-writing-sd-card-in-three-text-files-and-updating-the-files-diff-dat

Write file to sdcard in android

writing to sdcard

I use this code to create file and folder:

    try {
        File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
        if (!newFolder.exists()) {
            newFolder.mkdir();
        }
        Log.d(TAG, "TestFolder");
        try {
            file = new File(newFolder, "MyTest" + ".txt");
            file.createNewFile();
            Log.d(TAG, "MyTest");
        } catch (Exception ex) {
            System.out.println("ex: " + ex);
        }
    } catch (Exception e) {
        System.out.println("e: " + e);
    }

However it creates file in phone memory not sdcard. I used these permissions: Then I used this code to write a small string in the same text file:

    String str = "my_string";
    FileOutputStream outStream;
    try {
      outStream = openFileOutput(file.getName(), Context.MODE_PRIVATE);
      outStream.write(str.getBytes());
      outStream.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

But it doesn't write any thing in the text file file. What's wrong with my codes?

By the way I used these permissions too: READ_EXTERNAL_STORAGE,WRITE_EXTERNAL_STORAGE",READ_INTERNAL_STORAGE",WRITE_INTERNAL_STORAGE"

So it is not the matter of permission.

Community
  • 1
  • 1
Taher
  • 1,185
  • 3
  • 18
  • 36

2 Answers2

1

As the docs say, using Environment.getExternalStorageDirectory() does not guarantee that you'll get an external SD - it may, depending on the device, be an internal SD.

As for the file writing part of the issue, here's some working code from my app. It's very similar to your snippet but uses slightly different methods, and no explicit setting of permissions (which may be a factor):

    File mFile = new File(path);
    OutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(mFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        outputStream.write(obj.toString().getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        outputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }  
}
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • Thanks. I checked my app in two different devices which have SD card! What's the path variable in your code? Is it "Environment.getExternalStorageDirectory().getAbsolutePath()" ? – Taher Dec 24 '14 at 10:52
  • Yes, I'm getting it like this: `String fullFileName = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "myAppNameDirectory" + File.separator + myFilename;` Filename in my case is filepath and filename all as one string. – JASON G PETERSON Dec 24 '14 at 10:57
  • I used exactly your code but I got this error in log: java.io.FileNotFoundException: /mnt/sdcard/TestFolder/Test.txt: open failed: EACCES (Permission denied). I think it try to find file in external memory, while the app creates file in internal memory!!! What should I do for creating file and folder in external SD? My devices have external SD memory – Taher Dec 24 '14 at 13:13
  • Test with getExternalStorageState. If you're not getting back that it's mounted and read/ write capable, use internal storage instead.. – JASON G PETERSON Dec 24 '14 at 13:26
  • I checked it and it gave "mounted". Interestingly I used "/sdcard/" instead of getExternalStorageDirectory, but it created folder in internal memory!! – Taher Dec 24 '14 at 14:10
  • And make sure you've made your app directory first at the root of the external mount. – JASON G PETERSON Dec 24 '14 at 14:15
0

using Environment.getExternalStorageDirectory() does not guarantee that you'll get an external SD,if it have not a external SD card, you can use the app cache dir. context.getCacheDir();

Phil
  • 201
  • 1
  • 7