2016-07-27 96 views
0

專家,如何授予Android API 23的運行時權限?

下面是我的代碼,

問題描述:錯誤發生在db.execSQL聲明說,空db對象。

我試着檢查是什麼錯,並且應用了2個烤麪包,找出「test」不顯示,我的理解就是checkSelfPermission的判斷是錯誤的,所以我已經有了權限。但是,如果我直接寫db = SQLiteDatabase.openOrCreateDatabase(),它會有「未知錯誤(代碼14):無法打開數據庫」。

我真的沒有線索,請幫助,在此先感謝。

ActivityCompat.checkSelfPermission(this, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED 

代碼:

public class AbcActivity extends Activity { 

SQLiteDatabase db; 

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

    switch (requestCode) { 
     case 111222: 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
       db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null); 
      else { 
       System.exit(0); 
      } 
      break; 
    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_param); 

    if (ActivityCompat.checkSelfPermission(this, 
      Manifest.permission.WRITE_EXTERNAL_STORAGE) 
      != PackageManager.PERMISSION_GRANTED) { 
     Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show(); 

     if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        111222); 
     else 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        111222); 
    } 
    Toast.makeText(getApplicationContext(), "test2", Toast.LENGTH_SHORT).show(); 

    //db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null); 
    db.execSQL("create table if not exists " + "xxxxx" + "(" + 
      //<editor-fold desc="columns"> 
      "DtdKink REAL," + 
      "TttBin REAL" + 
      //</editor-fold> 
      ")"); 
} 

}

回答

1

我認爲這是不具有權限的關係,你應該使用SQLiteOpenHelper類使用SQLite。

例子:

public class FeedReaderDbHelper extends SQLiteOpenHelper { 
    // If you change the database schema, you must increment the database version. 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "FeedReader.db"; 

    public FeedReaderDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
} 

信息來源:

https://developer.android.com/training/basics/data-storage/databases.html

編輯:

在API 23 openOrCreateDatabase()不能創建,當你試圖在創建的文件夾外部存儲器,嘗試在調用此方法之前執行此操作

檢查此解決方案:

File root = new File(Global.dataFolder + File.separator); 
if (!root.exists()) { 
    //Create folder 
    root.mkdirs(); 
} 
db = SQLiteDatabase.openOrCreateDatabase(Global.dataFolder + File.separator + "xxxxx", null); 
       db.execSQL("create table if not exists " + "xxxxx" + "(" + 
       //<editor-fold desc="columns"> 
       "DtdKink REAL," + 
       "TttBin REAL" + 
       //</editor-fold> 
       ")"); 
+0

你能解釋一下什麼是錯的DB = SQLiteDatabase.openOrCreateDatabase說法嗎?它在API 21上運行成功。我開始擔心,既然你回答,人們會盡管回答,我會等待沒有正確的答案。 I – caibirdcnb

+0

openOrCreateDatabase(name,mode,factory)方法在Context類中,所以您應該使用基本活動SQLiteDatabase db = getActivity()。openOrCreateDatabase(「DATABASE」,android.content.Context.MODE_PRIVATE,null); –

+0

對不起,我沒有關注。正如我所提到的,代碼在API 21上工作。看起來你應該修改db語句,但「test」不執行,意味着沒有機會執行「db = SQLiteDatabase.openOrCreateDatabase」。這首先是一個邏輯問題。 – caibirdcnb