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:
priv
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.
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" />**
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);
}
...
...
}
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.
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/
Just give path in constructor ;
DatabaseHelper(Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DB_NAME, null, DB_VERSION);
}