Api 29(Android Q)的外部应用程序目录中的java SQLiteOpenHelper
我如何在安卓 Q(Api 29)上实现以下情况
我的目的是保存在一个目录中,该目录在卸载应用程序后将继续存在
它有以下类及其构造函数:
public class SqlHelper extends SQLiteOpenHelper {
//constructor
SqlHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
//rest of the class
}
在我的DAO中调用数据库时:
public class Dao {
private Context context;
protected SQLiteDatabase db;
public Dao(Context context) {
this.context = context;
}
protected void openDataBase() {
String pathDB = FOLDER_NAME_DBASE;
//getExternalStorageDirectory() deprecated Api 29
File dir = new File(Environment.getExternalStorageDirectory(), pathDB);
String dBasePath = dir.getPath() + "/" + mydatabse.db3;
SqlHelper varHelp = new SqlHelper(
context,
dBasePath,
null,
DBASE_VERSION
);
db = varHelp.getWritableDatabase();
}
//rest of the class
}
使用SQLiteOpenHelper有没有办法在Api 29的公共文档文件夹中创建数据库
谢谢大家的关注
# 1 楼答案
对于任何版本的Android来说,将实时SQLite数据库放在外部存储上都是不明智的。数据损坏的风险更大,因为其他应用程序可能会尝试使用该文件
但是,如果您希望忽略该建议,您可以使用Android 10上的
getExternalFilesDir()
作为数据库的基本目录。这是用户可以访问的,尽管位置不太友好