在api 23中无法写入SD卡

时间:2017-03-17 10:14:57

标签: java android android-permissions

我尝试在 Android 6.0 上的外部SD卡上写一切,我已经获得了许可。但如果我直接写G.database=SQLiteDatabase.openOrCreateDatabase(),它就会:

  

未知错误(代码14):无法打开数据库

G.class

    public static final String DIR_SDCARD= Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String DIR_DATABASE= DIR_SDCARD+"/CallBlocker/";

ActivitySplash

public class ActivitySplash extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{
private final int MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 10;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    if (checkPermission()) {
        File root = new File(G.DIR_DATABASE);
        if (!root.exists()) {
            //Create folder
            root.mkdirs();
        }
        G.database= SQLiteDatabase.openOrCreateDatabase(root+"/database.sqlite",null);
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS blacklist (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , num TEXT, name TEXT, sms TEXT, call TEXT)");
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS whitelist (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , num TEXT, name TEXT, sms TEXT, call TEXT)");
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS log (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , phone TEXT, sms TEXT, call TEXT, time TEXT, date TEXT)");

    }else {
        requestPermission();

    }

}
private void requestPermission(){

    if ((ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.WRITE_EXTERNAL_STORAGE))){
        File root = new File(G.DIR_DATABASE);
        if (!root.exists()) {
            //Create folder
            root.mkdirs();
        }
        G.database= SQLiteDatabase.openOrCreateDatabase(root+"/database.sqlite",null);
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS blacklist (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , num TEXT, name TEXT, sms TEXT, call TEXT)");
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS whitelist (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , num TEXT, name TEXT, sms TEXT, call TEXT)");
        G.database.execSQL("CREATE  TABLE  IF NOT EXISTS log (id INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL , phone TEXT, sms TEXT, call TEXT, time TEXT, date TEXT)");

    } else {

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

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

    switch (requestCode) {

        case MY_PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                finish();
                startActivity(getIntent());
            } else {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    new AlertDialog.Builder(this)
                            .setTitle("Read Contacts Permission")
                            .setMessage("You need to grant read contacts to use read"+"contacts fearure . Retry and  grant it !!")
                            .show();
                } else {
                    new AlertDialog.Builder(this)
                            .setTitle("Read Contacts Permission denied ")
                            .setMessage("You denied read Contacts Permission . so , the feature will be disabled . To enable it , go to settings and grant read contacts for the application")
                            .show();
                }

            }
            break;
    }

}
private boolean checkPermission(){
    int result = ContextCompat.checkSelfPermission(G.context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED){
        return true;
    } else {
        return false;
    }
}

的AndroidManifest.xml

    <uses-permission <android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

LOG

 java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.lswg.callblocker/ir.lswg.callblocker.ActivitySplash}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

0 个答案:

没有答案