31

Is there a way to store android application data on the SD card instead of in the internal memory? I know how to transfer the application sqlite database from the internal memory to the SDCard, but what if the internal memory gets full in the first place? How does everyone handle this?

6 Answers 6

76

It's better practice to use Environment.getExternalStorageDirectory() than to hard code "/sdcard" It's not always certain that the folder name will be called that. Also, the Environment class offers a getExternalStorageState() method to check on if the external storage is even available.

1
  • 2
    This doesn't always work because apparently Samsung's real SD Card location is Environment.getExternalStorageDirectory()+"/external_sd". Other devices will have different locations.
    – Muz
    Commented Mar 29, 2013 at 8:50
33

To begin:

Depending on the model/os, you can access the sd card root directory with:

File externalStorage = Environment.getExternalStorageDirectory();

This will refer to the internal sd storage or internal sd memory.

externalStorage.getAbsolutePath() 

will return one of the following values

"/sdcard/" or "/mnt/sdcard/"

To access the external sd memory or micro SD, that you usually plug from the outside of the phone/tablet, you must use one of the following folders that android creates to point to the external memory:

"/mnt/sdcard/sd" 
"/mnt/sdcard/external_sd"

"/sdcard/external_sd"
"/sdcard/sd" 

"/mnt/sdcard/"

ps: you can notice an empty folder external_sd or sd on the internal sdcard

memory, this folder is empty and its used to point to external micro sd card.

at the end make sure that you have read/write access to the sd card android.permission.WRITE_EXTERNAL_STORAGE in the android manifest xml.

finally you must specify the file name and your ready

private SQLiteDatabase DB = null;

private static final String DATABASE_NAME = "MyDb.db"; 
////////////
File sdcard = Environment.getExternalStorageDirectory();

String dbfile = sdcard.getAbsolutePath() + File.separator+ "external_sd" + File.separator + DATABASE_NAME;

DB = SQLiteDatabase.openDatabase(dbfile, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
///////////

and your ready to go ...

2
  • 2
    Unfortunately, this is absolutely not standard. On the LG Optimus it's in /mnt/sdcard/_ExternalSD. I hope someone at google is gonna review all that. Naming of media is so bad (external ???) and the docs don't tell a word about how to access second sdcard when there is one built-in.
    – Snicolas
    Commented Oct 3, 2011 at 12:02
  • Can you please help in getting all the Folders name present in the SD card. Commented Dec 13, 2012 at 7:00
13

Here is another neat little trick. The Application has a number of methods which are called to acquire paths. In particular the application has the method getDatabasePath with is used by SQLiteOpenHelper to construct the path. A custom application class can override these methods to provide different paths including paths in the getExternalStorageDirectory.

The external storage is either application specific or public. There are methods, replacing the getExternalStorageDirectory mechanism, getExternalFilesDir() and getExternalStoragePublicDirectory() respectively.

10

Warning: This answer is out-dated. You should use Environment.getExternalStorageDirectory() to get the root path of the SD card as mentioned in the answers below.

Old Answer so the comments on this make sense:

Adding /sdcard/ to the root your path should direct your Android application to use the SD card (at least it works that way with the G1). Android's file system objects give you the ability to check file sizes... so it should be possible (if tricky) to write some fail-over code. This code would adjust your root path if the internal memory filled up.

6
  • how do you adjust the root path?
    – user121196
    Commented Jul 30, 2009 at 21:51
  • Adjust the root path that you're using. (That was a bit confusing...you can't alter Android's root path)
    – haseman
    Commented Jul 30, 2009 at 23:46
  • I'm using SQLiteDatabase to create the database, however the name argument of it's constructor can't contain a path, how is it possible that I create the database on the sdcard?
    – user121196
    Commented Jul 31, 2009 at 5:00
  • 2
    developer.android.com/reference/android/database/sqlite/… Use the openDatabase call. It takes a path as the first parameter
    – haseman
    Commented Jul 31, 2009 at 16:28
  • what is I'm using a SQLiteOpenHelper, it doesn't take any path.
    – user121196
    Commented Jan 19, 2010 at 7:52
3

some device use /mnt/sdcard as root point to SD Card.

2

The problem with using the SDCard is that you cannot reliably assume that it will be present always when your application needs it. This is not the case with internal memory. As long as your application does not rely on this data to run it should be fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.