20

i want to create my sqlite database in sdcard instead of default path...i want to access all my data from sdcard also I have Used This Code:

            private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        SQLiteDatabase.openOrCreateDatabase("/sdcard/"+DATABASE_NAME,null);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + " (id INTEGER PRIMARY KEY, name TEXT, number TEXT, skypeId TEXT, address TEXT, image BLOB)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}

Problem:

When i see the database file in default path i can see all the data and table but when i see the database file created in sd card it doesnot shows any data but it only shows the database file

IN constructor it only creates the file in sdcard but in default path it does everything well..... How to store all Sqlitedata on sdcard for further access?

Shiv
  • 4,569
  • 4
  • 25
  • 39

6 Answers6

44

I created my DB with

    public DatabaseHelper(final Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + FILE_DIR
            + File.separator + DATABASE_NAME, null, DATABASE_VERSION);
}

and had no problems further on. I guess your call to super() should reference the sdcard as well.

koljaTM
  • 10,064
  • 2
  • 40
  • 42
  • Great @koljaTM :) it worked i was missing Environment.getExternalStorageDirectory() Thanx a lot :) – Shiv Jan 17 '13 at 08:18
  • @Shiv i had used this method for store db on sdcard now i want to restore one database to this location. can you have try this? – PankajAndroid Aug 22 '13 at 08:51
  • and second thing that every device has Environment.getExternalStorageDirectory()? this becuse i had create emulator with no sdcard not this will not work – PankajAndroid Aug 22 '13 at 10:22
  • Nice answer @koljaTM. But i have doubt FILE_DIR means what we need to give its a package name? – Android_dev Feb 13 '14 at 08:28
2

You are providing an incomplete name in your super() call. Try using:

OpenHelper(Context context) {
    super(context, "/sdcard/"+DATABASE_NAME, null, DATABASE_VERSION);
    SQLiteDatabase.openOrCreateDatabase("/sdcard/"+DATABASE_NAME,null);

}

Other than that, you should always use Environment.getExternalStoreDirectory() to get the path to the external storage, and you should also check the state of the external storage before attempting to use it.

Raghav Sood
  • 81,899
  • 22
  • 187
  • 195
1
public DataBaseHelper(final Context context) {

    super(context, Environment.getExternalStorageDirectory()
    + File.separator+ MYDATABASE_NAME, null, MYDATABASE_VERSION);
}

**Also Add permission in android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />**
sarath chambayil
  • 77
  • 1
  • 3
  • 11
0

Just give path in constructor ;

 DatabaseHelper(Context context) {
    super(context, Environment.getExternalStorageDirectory()
            + File.separator + "/DataBase/" + File.separator
            + DB_NAME, null, DB_VERSION);
}
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
0

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app.

For getting permissions at runtime, you will have to request the user. You can do that in following way.

First request for permissions.

String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        requestPermissions(permissions, WRITE_REQUEST_CODE);

And then you can check the results in

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
       case WRITE_REQUEST_CODE:
         if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
           //Permission granted.
           //Continue with writing files...
         }
       else{
           //Permission denied.
         }
        break;
    }
}

Here is good learning source requesting-runtime-permissions-in-android-marshmallow/

atiruz
  • 2,782
  • 27
  • 36
0

The use of Environment.getExternalStorageDirectory() method is deprecated,

This method was deprecated in API level 29. To improve user privacy, direct access to shared/external storage devices is deprecated. When an app targets Build.VERSION_CODES.Q, the path returned from this method is no longer directly accessible to apps. Apps can continue to access content stored on shared/external storage by migrating to alternatives such as Context#getExternalFilesDir(String), MediaStore, or Intent#ACTION_OPEN_DOCUMENT.

now we must use getExternalFilesDir(), this is an example:

public class MyDataBaseHelper extends SQLiteOpenHelper {

    //Constructor
    MyDataBaseHelper(Context context) {
    super(context, context.getExternalFilesDir()
            + File.separator + "/Database/" + File.separator
            + DATABASE_QUESTION, null, DATABASE_VERSION);
    }
   ...
   ...
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268