gpt4 book ai didi

android - 数据库在我的应用程序下载后似乎消失了。想法?

转载 作者:搜寻专家 更新时间:2023-10-30 23:23:08 30 4
gpt4 key购买 nike

我的一个应用程序从服务器下载数据库。当我将应用程序安装到我的手机上时,它会正确下载文件并加载信息,没有抛出任何异常或任何东西。

但是,当我将 apk 上传到 Android Market Place 并将其下载到手机上时,应用程序下载数据库然后崩溃,说 sqlite 处理程序无法打开数据库。

这是代码的进展:

在 SQLiteOpenHelper 中:

this.FULL_DB_PATH = new String(this.getWritableDatabase().getPath());
this.getWritableDatabase();

// Code for retrieving the database off of the server:

private void copyDataBase(){
InputStream myInput=null;
OutputStream myOutput=null;
try{
// opening connections
URL url = new URL("server_url_here");
URLConnection ucon=url.openConnection();
myInput = ucon.getInputStream();
myOutput = new FileOutputStream(this.FULL_DB_PATH);

// write to the db here
byte[] buffer = new byte[1024*1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();
}
catch(Exception e)
{

try{
myOutput.flush();
myOutput.close();
myInput.close();
}
catch(Exception es){}
}
}

我不知道为什么我的同事用 mp3 扩展名保存数据库,但是......当我们安装 apk ad-hoc 时它可以工作。

对于:

myOutput = new FileOutputStream(this.FULL_DB_PATH);

我也试过:

myOutput = this.getApplicationContext().openFileOutput(this.FULL_DB_PATH, Context.MODE_WORLD_READABLE);

但这也行不通。

非常感谢任何帮助!!我为此焦急了好几个小时,我只想结束它哈哈。

最佳答案

您正在做的是将文件复制到 android 文件系统中的/data 文件夹中。出于安全原因,在具有正常权限的普通电话上,这是不允许的。您可以在模拟器和 root 手机上执行此操作,因为您已被授予写入/data 的权限(通常只读)。

要解决这个问题,您需要使用 db.insert(String, String, ContentValues) 手动将信息添加到数据库中和 db.update(String, ContentValues, String, String[])

能够做您正在尝试的事情会很好,但出于安全原因,您不能这样做是一件非常好的事情。

关于android - 数据库在我的应用程序下载后似乎消失了。想法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3480015/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com