2015-11-09 46 views
4

我正在嘗試將備份和還原應用數據庫的功能合併到Google Drive應用文件夾(對用戶不可見)。我仔細閱讀了針對android給出的基本設置指南setup guide,並且可以讓用戶授權應用程序並達到調用onConnected方法的地步。將SQLlite數據庫備份/還原到Google Drive應用文件夾

我面對的問題是,我不知道如何去'發送'數據庫文件(.db)從設備到谷歌驅動器的應用程序文件夾。谷歌已經分享了創建一個新文件的代碼片段,但這就是它。我確實發現了一個先前提出的問題,與我正在尋找Google drive to back up and restore database and shared preferences of Android application的模式相似,但後來又不是我正在尋找的問題。

搜索谷歌沒有返回任何有用的鏈接,可能是因爲這是相對較新的api。

UPDATE 1:

共享onConnected()的代碼,

public void onConnected(Bundle bundle) { 
    Toast.makeText(GoogleSignIn.this, "In onConnected activity", Toast.LENGTH_SHORT).show(); 

    // Testing to see if database is indeed uploaded to google drive app folder 
    String db_name = "XXX_DB"; 
    String currentDBPath = "/data/" + "com.abc.efg" + "/databases/" + db_name; 
    saveToDrive(
      Drive.DriveApi.getAppFolder(mGoogleApiClientDrive), 
      "XXX_DB.db", 
      "application/x-sqlite3", 
      new java.io.File(currentDBPath) 
    ); 
} 

更新2:

以下共有的溶液完全適用於上載數據庫,以谷歌驅動的應用程序的文件夾。對於那些可能面臨類似問題的人,嘗試將數據庫路徑更改爲"/data/data/com.abc.efg/databases/" + db_name而不是"/data/com.abc.efg/databases/" + db_name

下一步是能夠從谷歌驅動器應用程序文件夾中檢索和恢復數據庫。如果我能夠得到它的工作,請更新這個問題。

回答

12

假設,你有你的MyDbFile.db文件的路徑,你可以使用一個結構是這樣的:

... 
saveToDrive(
    Drive.DriveApi.getAppFolder(getGoogleApiClient()), 
    "MyDbFile.db", 
    "application/x-sqlite3", 
    new java.io.File("\...\...\...\MyDbFile.db") 
); 
... 

DriveId mDriveId; 
/****************************************************************** 
* create file in GOODrive 
* @param pFldr parent's ID 
* @param titl file name 
* @param mime file mime type (application/x-sqlite3) 
* @param file file (with content) to create 
*/ 
void saveToDrive(final DriveFolder pFldr, final String titl, 
       final String mime, final java.io.File file) { 
    if (getGoogleApiClient() != null && pFldr != null && titl != null && mime != null && file != null) try { 
    // create content from file 
    Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveContentsResult>() { 
     @Override 
     public void onResult(DriveContentsResult driveContentsResult) { 
     DriveContents cont = driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ? 
      driveContentsResult.getDriveContents() : null; 

     // write file to content, chunk by chunk 
     if (cont != null) try { 
      OutputStream oos = cont.getOutputStream(); 
      if (oos != null) try { 
      InputStream is = new FileInputStream(file); 
      byte[] buf = new byte[4096]; 
      int c; 
      while ((c = is.read(buf, 0, buf.length)) > 0) { 
       oos.write(buf, 0, c); 
       oos.flush(); 
      } 
      } 
      finally { oos.close();} 

      // content's COOL, create metadata 
      MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build(); 

      // now create file on GooDrive 
      pFldr.createFile(getGoogleApiClient(), meta, cont).setResultCallback(new ResultCallback<DriveFileResult>() { 
      @Override 
      public void onResult(DriveFileResult driveFileResult) { 
       if (driveFileResult != null && driveFileResult.getStatus().isSuccess()) { 
       DriveFile dFil = driveFileResult != null && driveFileResult.getStatus().isSuccess() ? 
        driveFileResult.getDriveFile() : null; 
       if (dFil != null) { 
        // BINGO , file uploaded 
        dFil.getMetadata(getGoogleApiClient()).setResultCallback(new ResultCallback<MetadataResult>() { 
        @Override 
        public void onResult(MetadataResult metadataResult) { 
         if (metadataResult != null && metadataResult.getStatus().isSuccess()) { 
         DriveId mDriveId = metadataResult.getMetadata().getDriveId(); 
         } 
        } 
        }); 
       } 
       } else { /* report error */  } 
      } 
      }); 
     } catch (Exception e) { e.printStackTrace(); } 
     } 
    }); 
    } catch (Exception e) { e.printStackTrace(); } 
} 

/******************************************************************* 
* get file contents 
*/ 
void readFromGooDrive() { 
    byte[] buf = null; 
    if (getGoogleApiClient() != null && getGoogleApiClient().isConnected()) try { 
    DriveFile df = Drive.DriveApi.getFile(getGoogleApiClient(), mDriveId); 
    df.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null) 
     .setResultCallback(new ResultCallback<DriveContentsResult>() { 
     @Override 
     public void onResult(DriveContentsResult driveContentsResult) { 
     if ((driveContentsResult != null) && driveContentsResult.getStatus().isSuccess()) { 
      DriveContents cont = driveContentsResult.getDriveContents(); 
      // DUMP cont.getInputStream() to your DB file 
      cont.discard(getGoogleApiClient()); // or cont.commit(); they are equiv if READONLY 
     } 
     } 
    }); 
    } catch (Exception e) { e.printStackTrace(); } 
} 

好運

+0

欣賞代碼。至少現在我知道在哪個方向思考。試圖執行它,但我得到一個文件/文件夾未找到錯誤。使用onConnected方法代碼更新了問題。有趣的是,這與我用於在設備存儲中備份(和還原)數據庫的路徑相同,並且它沒有任何問題! – Paritosh

+0

這可能聽起來很天真,但路徑起作用,您的代碼使用「/ data/data /」代替「/ data /」!現在試圖扭轉過程,並從應用程序文件夾中獲取數據庫... – Paritosh

+0

我放在那裏的路徑(\ ... \ ... \)是象徵性的我不知道你的數據庫文件在哪裏,也不知道它的名字是什麼。該代碼僅處理將文件上傳到雲端硬盤。如果您需要將其下載回來,請使用上面獲得的DriveId並將其應用到'read()'方法[here](https://github.com/seanpjanson/GDAADemo/blob/master/app/src /main/java/com/spjanson/gdaademo/GDAA.java)。 – seanpj

相關問題