Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法写入api 23中的SD卡_Java_Android_Android Permissions - Fatal编程技术网

Java 无法写入api 23中的SD卡

Java 无法写入api 23中的SD卡,java,android,android-permissions,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 fina

我尝试在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/";
活动飞溅

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" />

G.DIR_数据库中的值是什么?@hareshchelana public static final String DIR_SDCARD=Environment.getExternalStorageDirectory().getAbsolutePath();公共静态最终字符串DIR_DATABASE=DIR_SDCARD+“/CallBlocker/”
DIR\u SDCARD=Environment.getExternalStorageDirectory()
。正如函数名告诉您的,这是外部内存。和搬家无关!e micro SD卡。
//创建文件夹root.mkdirs()。检查返回值。如果无法创建目录,If将为false。如果是的话,敬酒。然后再回来。试图在不存在的文件夹中创建数据库文件是没有意义的。
 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