I have this code to store a remote sqlite .db file with my android app:
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;
}
I need to open that file with my sqlitemanager, and I have this function, that opens a .db file from assets, but I need to adapt this method to load the file I stored in the previous mentioned method, which is not stored on assets folder. So I must change this method but I don't know how to do it correctly.
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");}
}
}