Reputation: 4567
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?
Upvotes: 20
Views: 37946
Reputation: 126455
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);
}
...
...
}
Upvotes: 0
Reputation: 2858
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/
Upvotes: 0
Reputation: 3422
Just give path in constructor ;
DatabaseHelper(Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DB_NAME, null, DB_VERSION);
}
Upvotes: 0
Reputation: 77
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" />**
Upvotes: 1
Reputation: 82543
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.
Upvotes: 2
Reputation: 10262
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.
Upvotes: 44