0

I'm working with Android and I'm trying to use a database I already have. I'd like to put it on the SD card. I have the database file in my project's assets folder. How can I make it on the SD card or external storage of whatever device the app is installed on?

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
muttley91
  • 12,278
  • 33
  • 106
  • 160

4 Answers4

2
//Step1 : Checked accessiblity on sd card
public boolean doesSDCardAccessible(){
    try {
        return(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));        
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();        
    }   
    return false;
    }

//Step2 : create directory on SD Card
//APP_DIR : your PackageName 
public void createAndInitAppDir(){
    try {
    if(doesSDCardAccessible()){
    AppDir = new File(Environment.getExternalStorageDirectory(),APP_DIR+"/");
    if(!AppDir.exists()){
        AppDir.mkdirs();    
    }
    }
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    }

//Step 3 : Create Database on sdcard
//APP_DIR : your PackageName 
//DATABASE_VERSION : give Database Vesrion
//DATABASE_NAME : your Databsename Name
public void initDB()
{
    try {

    //Using SQLiteHelper Class Created Database
    sqliteHelper = new SQLiteHelper(Application.this,AppDir.getAbsolutePath()+"/"+DATABASE_NAME, 
                                    null, DATABASE_VERSION);    

    //OR use following
    //Creating db here. or db will be created at runtime whenever writable db is opened.

    db = SQLiteDatabase.openOrCreateDatabase(AppDir.getAbsolutePath()+"/"+DATABASE_NAME,                                                     null);*/
    db= sqliteHelper.getWritableDatabase();
    db.close();
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Mitalkumar
  • 36
  • 2
0

In reference to this answer, you can find the directory of the SD card in Android 4.0+ by trying both of the following (only one should work per device):

new File("/mnt/external_sd/");

or

new File("/mnt/extSdCard/");

On Android <4.0, you can use

Environment.getExternalStorageDirectory();

You can create your SQLite database there. Additionally, if you can't find it, you can iterate over all directories in /mnt/ (note: the sdcard will always be accessible via /mnt/).

Community
  • 1
  • 1
0

The database is like any other flat file. Just copy it to your SD card.

public boolean backup()
{
    File sdcard = Environment.getExternalStorageDirectory();
    File data = new File("/data/data/com.mydomain.myappname/databases/");

    if (sdcard.canWrite())
    {
    File input = new File(data, DB_NAME);
    File output = new File(sdcard, "android/data/com.mydomain.myappname/databases/");

    if(!output.exists())
    {
        if(output.mkdirs())
        {
            output = new File(sdcard,
            "android/data/com.mydomain.myappname/databases/backup.db");
            output.createNewFile();
            result = true;
        }
        else
        {
            output = new File(sdcard,
            "android/data/com.mydomain.myappname/databases/backup.db");
            result = true;
        }

        if(input.exists() && result)
        {
            FileInputStream source;
            FileOutputStream destination;

            try
            {
                source = new FileInputStream(input);
                    try
                    {
                        destination = new FileOutputStream(output);
                        byte[] buffer = new byte[1024];
                        int length;

                        while((length = source.read(buffer)) > 0)
                        {
                            destination.write(buffer, 0, length);
                        }

                        source.close();
                        destination.flush();
                        destination.close();
                        result = true;
                   }
                   catch(Exception e)
                   {
                       result = false;
                       destination = null;
                   }
               }
               catch(Exception e)
               {
                   result = false;
                   source = null;
               }
           }
           else
           {
               result = false;
           }
       }
       else
       {
           result = false;
       }
    }
    catch(Exception e)
    {
        result = false;
    }
    return result;
}
MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • And what, exactly, is not working? If you don't describe the problem, don't be surprised when you don't get an answer. – MarsAtomic Apr 24 '13 at 06:39
  • Of course. I'm just having trouble figuring out what's going on myself. For some reason, the DataBaseHelper class I found here: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ refuses to create the database...even though I was able to create it on other devices. Do I need certain permissions to be able to create the database in the location specified in that code? – muttley91 Apr 24 '13 at 07:08
  • It's actually strange. I can run createDatabase() and openDatabase() just fine, as well as getReadableDatabase(). But then when I try to query it, it claims the columns don't exist...so either the database didn't create properly or it didn't create at all. But...why wouldn't it return an error? – muttley91 Apr 24 '13 at 07:11
0

Go throuh this link

OR try following

 InputStream myInput;

        try {

                     AssetManager assetManager = getAssets();
            myInput =  assetManager.open("mydatabase.db");


            File directory = new File("/sdcard/some_folder");

            if (!directory.exists()) {
                directory.mkdirs();
            }

            OutputStream myOutput = new FileOutputStream(directory
                    .getPath() + "/DatabaseSample.backup");

            byte[] buffer = new byte[1024];
            int length;
            while ((length = myInput.read(buffer)) > 0) {
                myOutput.write(buffer, 0, length);
            }

            myOutput.flush();

            myOutput.close();

            myInput.close();

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
Community
  • 1
  • 1
Shiv
  • 4,569
  • 4
  • 25
  • 39