Search code examples
androidsqlitegoogle-drive-apigoogle-drive-android-api

How to download file from Google Drive and restore db from it


Using the quickstart provided by Google I managed to upload a copy of my SQLite DB to my Google Drive properly. My only problem now is getting it back and restoring using that file.

I read here that since I've already uploaded the file I should have access to it as well. So far I can follow that, but when it goes down to reading the contents I get confused.

I'm still new so I can barely understand the processes I see on the demos, so far from what I can tell all I can do right now is ( I hope I'm actually doing it, I just follow the demos ) locate it. So now I'm stuck at HOW do I get it back? Can anyone lend a hand?

My original plan was to just download the file into a specified folder on my SD and just restore from that since I already know how to do backups and restoration from SD.


Solution

  • You can pull down the GDAA CRUD wrapper from here. It uses the simplest form of GDAA calls, the 'await' flavor. As long as you wrap any of these calls to non-UI threads (Thread, AsyncTask) you should be OK.
    If you decide to go this way (see how GDAA class communicates with MainActivity - connection, authorization), you basically need 3 methods:

    1. search for your file using it's title, mime, or whatever criteria you have, resulting in a valid DriveId. You may also use the DriveId you obtained from previous upload.
    2. read the file content into a byte[] buffer
    3. save your buffer into a DB file on your android device.

    The demo shows uploading / downloading of JPEG images to/from the Drive (disregard the REST branch, it is there only for testing). You're dealing with a DB file, but in the end it still is a buffer/stream full of binary data that is uploaded/downloaded (with some metadata like title, mimetype,...).

    Good Luck