1

I am trying to save a file on both internal and external sd card .. basically I want to give user an option to save file on internal or external cards This is how I am creating file in Internal SD card

File file = new File(Environment.getExternalStorageDirectory()
            + "/Tnt_input/register.html");

but how to create this file in External SD card...?

I can see two storage in DDMS

1) sdCard0

2) extSdCard

by using above code its creating this file in sdCard0

revo
  • 47,783
  • 14
  • 74
  • 117
AddyProg
  • 2,960
  • 13
  • 59
  • 110

3 Answers3

3

You can use context.getFilesDir() for save file in Internal Storage

File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;

try {
  outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
  outputStream.write(string.getBytes());
  outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

Check saving a file to internal storage for more information.

Note: Your app's internal storage directory is specified by your app's package name in a special location of the Android file system. Technically, another app can read your internal files if you set the file mode to be readable.

Good Example for Save and read file from/to Internal/External storage

how to differentiate between internal and external SD card path in android?

Community
  • 1
  • 1
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133
  • this creates file in phone's internal memory which is only assessable by application. i want to create file in removable memory card.. my phone has 8gb internal storage from which 5.8gb is user accessible and code mentioned in question creates file in this internal storage.. – AddyProg May 05 '14 at 10:32
  • @AdilWaqar [check this question](http://stackoverflow.com/questions/5694933/find-an-external-sd-card-location/5695129#5695129) – Niranj Patel May 05 '14 at 10:52
  • means there is no way i can differentiate between Internal SdCard and External SdCard in android as my app is targeting 4.0 and min sdk version is 2.3... – AddyProg May 05 '14 at 11:51
  • 1
    @AdilWaqar I found out [this](http://stackoverflow.com/questions/16688989/how-to-differentiate-between-internal-and-external-sd-card-path-in-android) may be this will helpful – Niranj Patel May 05 '14 at 12:31
  • it helped :) at least now i can check if there is an external sd card installed or not by using Environment.isExternalStorageRemovable(); but to get its path to store files i have to do more work.... – AddyProg May 05 '14 at 12:46
  • @AdilWaqar okey fine..if you got proper solution then please share. – Niranj Patel May 05 '14 at 12:57
0

Until Android 4.4 there was no standard way to find all external SD card memories, only one such memory was returned by Environment.getExternalStorageDirectory(). To get to the other memories programmers were parsing some linux configuration files.

Since 4.4 (API 19) there is: Context.getExternalFilesDir which:

Returns absolute paths to application-specific directories on all external storage devices where the application can place persistent files it owns. These files are internal to the application, and not typically visible to the user as media.

so on erlier than 4.4 you should use Environment.getExternalStorageDirectory, and after 4.4 Context.getExternalFilesDir.

There is a good blog post explaining all of this in detail:

http://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html

marcinj
  • 48,511
  • 9
  • 79
  • 100
0

Finally found a working solution , but its not recomended, to get to external sd card i used hard coded path , like /storage/extSdCard/StorageTest/input but this path depends upon device , the above path works in Samsung Galaxy note series but for xperia Z its /storage/removable/sdcard1. This solution worked for me because my client use a specific device.But like this you cant create a global method which works on every device, so here is the code which worked for me

String galaxy_note = "/storage/extSdCard";
File file = new File(galaxy_note
    +"/StorageTest/input");

you can also check if there is a removable sd card installed in device or no by using

Environment.isExternalStorageRemovable();
AddyProg
  • 2,960
  • 13
  • 59
  • 110