我有这段代码可以用我的 android 应用程序存储一个远程 sqlite .db 文件:
public String getRemoteCollection(String url, String dbName){
BufferedInputStream in = null;
FileOutputStream fout = null;
dbName="myFile";
try
{
in = new BufferedInputStream(new URL(url).openStream());
fout = SectionManager.instance.activity.openFileOutput(dbName, Context.MODE_PRIVATE);
byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1)
fout.write(data, 0, count);
if (in != null)
in.close();
if (fout != null)
fout.close();
}
catch(Exception e){e.printStackTrace();}
return dbName;
}
我需要用我的 sqlitemanager 打开那个文件,我有这个功能,它从资产中打开一个 .db 文件,但我需要调整这个方法来加载我在前面提到的方法中存储的文件,它不存储在资产上文件夹。所以我必须改变这个方法,但我不知道如何正确地做到这一点。
private void generateSQLiteDB(String databaseName) { //
SQLiteDatabase db = dbHelper.getReadableDatabase(); // by calling this line an empty database will be created into the default system path of this app - we will then overwrite this with the database from the server
db.close();
OutputStream os = null;
InputStream is = null;
try{
is = ctx.getAssets().open(databaseName+".db");
String packageName=SectionManager.instance.activity.getApplicationContext().getPackageName();
//os = new FileOutputStream("/data/data/com.android.launcher/databases/"+databaseName+".db");
os = new FileOutputStream("/data/data/"+packageName+"/databases/"+databaseName+".db");
copyFile(os, is);
}catch (Exception e) {
Log.e("DB", "Database not found", e);
}finally{
try{
//Close the streams
if(os != null)
os.close();
if(is != null)
is.close();
} catch (IOException e) {Log.e("DB", "Can't close adapters");}
}
}