Question

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

Was it helpful?

Solution 3

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();

OTHER TIPS

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?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top