1

Experts,

I have this error when run SQLiteDatabase.openOrCreateDatabase, on API 23 only, ob API 21 is ok.

According to previous question link below, the answer from AB1209 pointed that should grant run time permission. However, this is API 23 new function.

I would like to use the app on both API 21 and 23. I have 2 question:

  1. Isn't there a logic issue? I already grant permission in manifest, what is the good reason to do this on more time at run time? (My understanding is I need run time permission only when I did not grant in manifest during the install)

  2. If I grant permission at run time, will it affect the usage when run on API 21? (I can still use the APP on API 21?)

Thanks.

Link: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

I already have below permission:

 android.permission.WRITE_EXTERNAL_STORAGE"
Community
  • 1
  • 1
caibirdcnb
  • 275
  • 5
  • 18
  • 1
    I'd suggest you research on the topic of runtime permissions, probably starting here: https://developer.android.com/training/permissions/requesting.html. You obviously don't request them prior to 23, but you absolutely have to on > 23. – Egor Jul 26 '16 at 10:33
  • In my link, AB1209 has a link in his reply, I opened it and it begins: ...not only simplify permissions, but also to reduce the number of permissions needed... So I thought it is to simply things, now I understand it is to replace current. So I think my question solved. I just need to use both way, so it can be use for both API. – caibirdcnb Jul 26 '16 at 10:51

1 Answers1

0

Put this in your activity:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);

And your code here:

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        //resume tasks needing this permission
    }
}
Nir Duan
  • 6,164
  • 4
  • 24
  • 38