Running latest version of Android Studio. Project is built for API level 23 and up (Marshmallow) and using a Pixel XL emulator. I have granted permissions in Manifest and obtaining write access at runtime too (as required for API 23 and above)
Have a button hooked upto this
public void createDatabase(View view) {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions, 1);
}
I also have this in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is my code for permission grant
@Override
public void onRequestPermissionsResult(int requestCode, String
permissions[], int[] grantResults) {
switch (requestCode) {
case 1:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
DbUtil.CreateDb(getApplicationContext().getApplicationInfo().dataDir + "/databases/game360.db");
}
else{
//Permission denied.
}
break;
}
}
And this is DBUtil.CreateDb
public static void CreateDb(String dbPath){
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);
db.execSQL("CREATE TABLE if not exists user (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"username VARCHAR NOT NULL unique, firstName VARCHAR NOT NULL, lastName VARCHAR NOT NULL)");
ContentValues values = new ContentValues();
values.put("username", "jj678");
values.put("firstName", "brad");
values.put("lastName", "pitt");
db.insert("user",null, values);
db.close();
}
When I run app on emulator and click button, I get the permissions popup as expected. After I grant permission o, create Database gets called and it fails with below stack trace (error code 14). Any idea why?
02-23 17:44:39.347 19358-19358/com.jtech.game360 E/SQLiteLog: (14) cannot open file at line 31278 of [2ef4f3a5b1]
02-23 17:44:39.347 19358-19358/com.jtech.game360 E/SQLiteLog: (14) os_unix.c:31278: (2) open(/data/user/0/com.jtech.game360/databases/game360.db) -
02-23 17:44:39.348 19358-19358/com.jtech.game360 E/SQLiteDatabase: Failed to open database '/data/user/0/com.jtech.game360/databases/game360.db'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:207)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:709)
at com.jtech.game360.DbUtil.CreateDb(DbUtil.java:13)
at com.jtech.game360.Main2Activity.onRequestPermissionsResult(Main2Activity.java:52)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
at android.app.Activity.dispatchActivityResult(Activity.java:6432)
com.jtech.game360.DbUtil.CreateDb(), but it's a bit hard to know, since we can't see the code. Also I think you don't need permissions to write inside app's root directory (/data/user/0/com.jtech.game360)