Using Google Drive to backup and restore SQLite Database

后端 未结 2 1339
温柔的废话
温柔的废话 2020-12-29 09:44

I\'ve managed to create a backup of my database on an SD card and restore from there but realized that the purpose of my backup is to ensure the safety of the data and in th

2条回答
  •  生来不讨喜
    2020-12-29 10:13

    Both APIs used to access Google Drive deal with a binary content. So the only thing you have to do is to upload your binary DB file, give it a proper MIME type and a NAME (title).
    The selection of API depends on you, GDAA behaves like a 'local' entity with uploads / downloads handled by Google Play Services, REST Api is more low-level, giving you more control, but you have to take care of networking issues (wifi on/off, etc), i.e. you usually have to build a sync service to do so. With GDAA it is done for you by GooPlaySvcs. But I digress.
    I can point you to this GitHub demo, fairly recent (GooPlaySvcs 7.00.+), I use to test different REST / GDAA issues. The MainActivity is a bit complicated by the fact that it allows for switching between different Google accounts, but if you get through these hurdles, you can use either REST or GDAA CRUD wrappers.

    Take look at this line. The byte[] buffer contains binary JPEG data and it goes with "image/jpeg" mime type (and a time-based name). The only thing you have to do if is load your DB file into a byte[] buffer using a construct like this:

      private static final int BUF_SZ = 4096;
    
      static byte[] file2Bytes(File file) {
        if (file != null) try {
           return is2Bytes(new FileInputStream(file));
        } catch (Exception ignore) {}
       return null;
      }
    
      static byte[] is2Bytes(InputStream is) {
        byte[] buf = null;
        BufferedInputStream bufIS = null;
        if (is != null) try {
          ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
          bufIS = new BufferedInputStream(is);
          buf = new byte[BUF_SZ];
          int cnt;
          while ((cnt = bufIS.read(buf)) >= 0) {
            byteBuffer.write(buf, 0, cnt);
          }
          buf = byteBuffer.size() > 0 ? byteBuffer.toByteArray() : null;
        } catch (Exception e) {le(e);}
        finally {
          try {
            if (bufIS != null) bufIS.close();
          } catch (Exception e) {le(e);}
        }
        return buf;
      }
    

    I don't remember the MIME type for SQLite DB now, but I am sure it can be done since I was doing exactly that once (the code is gone now, unfortunately). And I remember I could actually access and modify the SQLite DB 'up in the cloud' using some web app.

    Good Luck

    UPDATE:
    After I wrote the rant above I looked at the demo you're talking about. If you have it working, the easiest way is actually to plug your DB file right here, set the correct MIME and you're good to go. Take you pick.
    And to address your 'deprecated' issue. GDAA is still being developed and the quickstart is over a year old. That's the world we live in :-)

提交回复
热议问题