无法检索数据库,并显示一条lint警告:
Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
尝试过搜索相关错误,但没有任何帮助。 这是代码
private static String DB_PATH = "/data/data/mlearning.fundprog/databases/";
private static String DB_NAME = "questionsDb";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBHelper(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + DB_NAME;
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
我真的不明白这有什么不对。之前它正在工作,但我正在思考它,因为我已经从API 8转移到API 14.我想这个更高的API有另一个编码规则。是这样吗?
答案 0 :(得分:3)
获取数据库的路径use getDatabasePath()
。
答案 1 :(得分:1)
您可以使用以下任何一种方法来获取数据库的路径:
DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases";
或者使用这个
DB_PATH = context.getDatabasePath(DB_NAME).getParent();