2014-12-04 105 views
0

在我的應用程序中,我需要將多個文件(1個sqlite數據庫文件和多個圖像文件)上傳到用戶的谷歌驅動器。 我正在使用android谷歌驅動器API,但不知道,如何做背靠背文件上傳,然後再下載像這樣。重複上傳和下載多個文件 - Android Google Drive api

db文件明顯來自/ data/data //數據庫類目錄,而圖像存儲在圖片目錄中。我需要逐一抓住所有這些,並上傳到驅動器。

而且,我已經看到,如果給定的文件(使用相同的標題)已經存在,即使在當時,用相同的標題一個新的文件在驅動器中創建(顯然有差異,但驅動器Id標題相同)。我想檢查一下,如果文件存在,只上傳,如果不存在,則跳過該文件。

請幫助..我一直在試圖通過谷歌指在GitHub上的Android演示,但一直只能夠做到使用的點點滴滴。

回答

2

文件標題在Drive API中不唯一。但是,您可以將新創建​​的文件的ID保存在應用程序的本地存儲中,以便您可以在再次上傳文件時檢查驅動器端的ID。

您可以從谷歌驅動器演示GitHub的頁面使用CreateFileActvity.java。在成功創建文件後,它會返回一個文件ID,以便您可以將ID存儲在本地存儲中。從CreateFileActivity.java

示例代碼:

final private ResultCallback<DriveFileResult> fileCallback = new 
      ResultCallback<DriveFileResult>() { 
     @Override 
     public void onResult(DriveFileResult result) { 
      if (!result.getStatus().isSuccess()) { 
       showMessage("Error while trying to create the file"); 
       return; 
      } 
      showMessage("Created a file with content: " + result.getDriveFile().getDriveId()); 
     } 
    }; 
+0

我很喜歡使用github示例中給出的方法,但是沒有搜索結果顯示我如何重複上載文件。這種方法的問題在於,只要寫入文件並返回fileid,連接就會關閉。我想一個接一個地上傳多個文件,但我一直無法找到解決方法。 – 2014-12-09 19:02:57

2

萬一有人正在尋找如何上傳多個文件到驅動器,這裏是解決方案,爲我工作:

for(String fileName: fileNameArrayList){backupImage(fileName);} 

private void backupImage(String fileName) { 
    Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(
      new BackupImagesContentsCallback(mContext, mGoogleApiClient, fileName)); 
} 

備份回調:

public class BackupImagesContentsCallback implements ResultCallback<DriveApi.DriveContentsResult> { 

@Override 
public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) { 
    if (!driveContentsResult.getStatus().isSuccess()) { 
     Log.v(TAG, "Error while trying to backup images"); 
     return; 
    } 

    MetadataChangeSet changeSet = new MetadataChangeSet.Builder() 
      .setTitle(mFileName) // Google Drive File name 
      .setMimeType("image/jpeg") 
      .setStarred(true).build(); 

    Drive.DriveApi.getAppFolder(mGoogleApiClient) 
      .createFile(mGoogleApiClient, changeSet, driveContentsResult.getDriveContents()) 
      .setResultCallback(backupImageFileCallback); 
} 

final private ResultCallback<DriveFolder.DriveFileResult> backupImageFileCallback = new ResultCallback<DriveFolder.DriveFileResult>() { 
    @Override 
    public void onResult(@NonNull DriveFolder.DriveFileResult result) { 
     if (!result.getStatus().isSuccess()) { 
      Log.v(TAG, "Error while trying to backup images"); 
      return; 
     } 
     DriveFile mImageFile; 
     mImageFile = result.getDriveFile(); 
     mImageId = result.getDriveFile().getDriveId(); 
     mImageFile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, (bytesDownloaded, bytesExpected) -> { 
     }).setResultCallback(backupImagesContentsOpenedCallback); 
    } 
}; 

final private ResultCallback<DriveApi.DriveContentsResult> backupImagesContentsOpenedCallback = 
     new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(@NonNull DriveApi.DriveContentsResult result) { 
       if (!result.getStatus().isSuccess()) { 
        return; 
       } 

       DriveContents contents = result.getDriveContents(); 
       BufferedOutputStream bos = new BufferedOutputStream(contents.getOutputStream()); 
       byte[] buffer = new byte[1024]; 
       int n; 

       File imageDirectory = new File(mContext.getFilesDir(), 
         Constants.IMAGE_DIRECTORY_NAME); 

       try { 
        FileInputStream is = new FileInputStream(new File(imageDirectory, 
          mFileName)); 
        BufferedInputStream bis = new BufferedInputStream(is); 

        while ((n = bis.read(buffer)) > 0) { 
         bos.write(buffer, 0, n); 
        } 
        bos.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 

       contents.commit(mGoogleApiClient, null); 
      } 
     }; 
} 

這不是完美的解決方案,只是一個工作代碼。

相關問題