-1

I have a requirement to save a file in Android external SD card. I have tried using this source code but it saves file in internal memory of phone.

Here is my code.

try {

    File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");

    if (!newFolder.exists()) {
        newFolder.mkdir();
    }

    File file = new File(newFolder, "MyTest" + ".txt");
    if (!file.exists()) {

        file.createNewFile();

    }

    Toast.makeText(MainActivity.this, "Created Successfully!", Toast.LENGTH_SHORT).show();

} catch (Exception e) {
    e.printStackTrace();
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Robin Royal
  • 1,788
  • 1
  • 18
  • 29
  • 1
    Add your manifest – Zoe Sep 12 '17 at 15:25
  • Possible duplicate of [Android how to use Environment.getExternalStorageDirectory()](https://stackoverflow.com/questions/5453708/android-how-to-use-environment-getexternalstoragedirectory) – nikis Sep 12 '17 at 15:26
  • "Every Android-compatible device supports a shared "external storage" that you can use to save files. This can be a removable storage media (such as an SD card) or an internal (non-removable) storage. Files saved to the external storage are world-readable and can be modified by the user when they enable USB mass storage to transfer files on a computer." Source: https://developer.android.com/guide/topics/data/data-storage.html#filesExternal – Eugen Pechanec Sep 12 '17 at 15:27

2 Answers2

0

From the documentation (https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory())

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

Tobias Uhmann
  • 2,757
  • 2
  • 25
  • 35
0

The storage you are referring as internal is actually shared storage and Android treats it as external storage. It depends upon user settings whether he want to store your application folder in sd card or Android shared space.

Deepak Vajpayee
  • 348
  • 2
  • 4
  • 15