无法检索数据库,并显示以下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有另一个编码规则。是真地吗?
发布于 2014-08-30 22:33:14
要获取数据库的路径,请使用use getDatabasePath()。
发布于 2014-08-30 23:15:18
您可以使用以下两种方法中的任何一种来获取数据库的路径:
DB_PATH = "/data/data/" + context.getApplicationContext().getPackageName() + "/databases";或者使用这个
DB_PATH = context.getDatabasePath(DB_NAME).getParent();https://stackoverflow.com/questions/25583456
复制相似问题