Tuesday, April 26, 2011

Android development

android development:
access sql lite database on  sd card:

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,
 you must use one of the following folders, 
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.
finaly you must specify the file name and your ready 
private SQLiteDatabase DB = null;
private static final String DATABASE_NAME = "MyDb.db"; 
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 ...