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");}
}
}