我正在尝试使用SQLiteAssetHelper将预先填充的数据库复制到Android应用程序中。我的设备上安装了Android-7(Nvidia Shield和摩托罗拉手机)。从设置 - > App-> MyApp我启用了日历,位置& 存储权限
我在Manifest文件中也有以下权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
当我将我的数据库保存在assets/databases/databases
文件夹中时,我得到以下异常:
引起:com.readystatesoftware.sqliteasset.SQLiteAssetHelper $ SQLiteAssetException:无法将/data/user/0/com.funcheap.funmapsf/databases/databases/MySFfuncheapDatabase.db写入数据
当我将我的数据库保存在assets/databases
文件夹中时,我得到以下异常:
引起:com.readystatesoftware.sqliteasset.SQLiteAssetHelper $ SQLiteAssetException:资产中缺少数据库/数据库/ MySFfuncheapDatabase.db文件(或.zip,.gz存档)或目标文件夹不可写
不幸的是,这SO question对我没有帮助
答案 0 :(得分:0)
正如@miket所说,如果你引用内部路径Android将使用该引用我遇到了这个问题,这是我非常草率的测试代码注释掉了调用
public void getINTERNAL(){
Context context = this;
File file = new File(context.getFilesDir().getAbsolutePath());
String NT = String.valueOf(file);
THE_PATH = NT+"/";
System.out.println("INTERNAL PATH =========> "+THE_PATH);
}
public void getAvailable(){
//getINTERNAL();
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED) && (!state.equals(Environment.MEDIA_MOUNTED_READ_ONLY))) {
File removable = ContextCompat.getExternalFilesDirs(this, null)[1];
THE_PATH = String.valueOf(removable);
if(THE_PATH.length() > 4) {
//THE_PATH = THE_PATH.substring(0, THE_PATH.length() - 5);
THE_PATH = THE_PATH + "/Documents/";
System.out.println("new path ====> "+THE_PATH);
}
System.out.println("EXTERNAL PATH ====> " + THE_PATH);
System.out.println("Internal Path used if EXTERNAL PATH = NULL ");
//getINTERNAL(); DO NOT CALL HERE YOU WILL CHANGE THE_PATH TO internal
// no need to set THE_PATH for internal storage it will default on its own
// WHY DOES THIS HAPPEN THE_PATH variable is PUBLIC STATIC hence it is GLOBAL
}
}