Issue
I have an app which has a .db (sql database) inside the "assets" folder. When I need to update this database, I simply copy the new version in the "assets" folder, change the user version of the .db file and rebuild the apk. At runtime the application deletes the file in memory and copy exactly the one in the "assets" folder, then it checks if the version of this file is greater than the last one it had in memory (in another database) and, in that case, it does some stuff (which I'm skipping, since is not the point of the question).
The problem is: my application seems to ignore this new file, maintaining the old file in the "assets" folder, so naturally no change is detected. It seems like my new file is not present in the apk, but only in devices with Android 9.
Is something changed with the usage of the assets folder that I am missing? How can I arbitrarily update a file inside my apk when packing and upgrade?
This exact process works fine for older devices, then I had to upgrade the target sdk version to 28 and I got this problem while testing on an Android 9 (Pie) device.
Here I delete older file:
File l_DatabaseDir = p_Context.getDir("Database", Context.MODE_PRIVATE);
File l_DatabaseFile = new File(l_DatabaseDir, "MainDatabase.db");
File l_PlayersFile = new File(l_DatabaseDir, "players.db");
l_PlayersFile.delete();
Here I copy the file from the assets folder:
try
{
InputStream inputStream = p_Context.getAssets().open("players.db");
//Create the file before
OutputStream outputStream = new FileOutputStream(l_PlayersFile);
byte[] buffer = new byte[4096];
int length;
while ((length = inputStream.read(buffer)) > 0)
{
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e)
{
e.printStackTrace();
}
Finally I try to get the current version of the db:
m_PlayersDatabase = SQLiteDatabase.openDatabase(l_PlayersFile.toString(), null, SQLiteDatabase.OPEN_READWRITE);
int playersVersion = m_PlayersDatabase.getVersion();
Result on android 9: db version is the same as the first apk installed
Result on android 8: db version is the one of the file included in the new apk
EDIT: after further investigating it seems that the problem is more related to android memory, because simply changing the database file name (basically the destination of the copy)
l_PlayersFile = new File(l_DatabaseDir, "newplayers.db");
makes the application read the correct file also in android 9, so it looks like maintaining the same file name as the old one is causing the problem (Even if both the deletion and the copy of the new file are completed successfully). So the assets folder update is fine. The issue is only postponed to the next update though, since using again the name "newplayers.db" causes the exact same problem. Generating a random name at every opening is quite brutal as workaround, any ideas?
Solution
Solved as I stated in the edit: copy the file from the assets folder with a different name (in my case with a timestamp).
Answered By - Simone Mariottini
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.