Search code examples
androidandroid-sqlitegoogle-play-games

Creating a byte array from an SqliteDatabase to save it with Google Saved Games?


I have a game where I use a couple of tables to store player's progress. It is quite long game so from time to time I'm having this legitimate user feedback about why I don't have cloud save option?

Therefore I do use Google Play Games sign in to have achievements, I thought I could simply implement Google Saved Games as well, straightforward.

As I see, it uses a simple byte[] array to save & load game progression from the cloud. (Google Drive, actually.)

My question is, how can I create a byte[] array which holds the FULL database of a player's saved game instance?

I'm using Sqlite Database like:

private static DatabaseHelper mInstance = null;


private DatabaseHelper(Context context) {
    super(context, "duels_rpg_db", null, 123);
  

    this.ctx = context;

 
    getWritableDatabase();
}


public static DatabaseHelper getInstance(Context ctx) {

   
    if (mInstance == null) {
        mInstance = new DatabaseHelper(ctx.getApplicationContext());
    }
    return mInstance;
}

and so on, nothing extraordinary.

Thanks in advance,


Solution

  • It would be a good idea to serialize your user data instead of the db file instead. This way if you update your app, you wont app to worry about using a newer version of the DB in a not-yet-updated version of the app.

    So I would recommand convert your progress to and from org.json.JSONObject. With the JSON, it's easy to convert to an byteArray with json.toString().getBytes("utf-8")

    On fetching the data, you can convert it back to json and wirrrte a method that load your sqlite db with the data of the json