Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

storing android application data on SD Card

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?

like image 875
user121196 Avatar asked Jul 30 '09 21:07

user121196


People also ask

Can app data be stored on SD card?

You can move apps to an SD card from the Apps section of an Android phone's Settings with just a few taps. If your Android phone has an SD card slot, you can move apps out of internal storage. Storing apps on an SD card can free up space on your phone for other apps and data.

Can you move app data to SD card Android?

Go to Settings > Apps and tap the app you want to move to your SD card. Next, under the Storage section, tap Move to SD Card. The button will be grayed out while the app moves, so don't interfere until it's done. If there's no Move to SD Card option, the app cannot be moved.

Why can't I move apps to my SD card Android?

Reason 1.Developers of Android apps need to explicitly make their apps available to move to the SD card using the “android:installLocation” attribute in the <manifest> element of their app. If they don't, the option to “Move to SD card” is grayed out.


4 Answers

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.

like image 61
yincrash Avatar answered Oct 12 '22 09:10

yincrash


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 ...

like image 37
Rabih harb Avatar answered Oct 12 '22 11:10

Rabih harb


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.

like image 39
phreed Avatar answered Oct 12 '22 10:10

phreed


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.

like image 43
haseman Avatar answered Oct 12 '22 11:10

haseman