重复上传和下载多个文件 - Android Google Drive api

问题描述:

在我的应用程序中,我需要将多个文件(1个sqlite数据库文件和多个图像文件)上传到用户的谷歌驱动器。 我正在使用android谷歌驱动器API,但不知道,如何做背靠背文件上传,然后再下载像这样。重复上传和下载多个文件 - Android Google Drive api

db文件明显来自/ data/data //数据库类目录,而图像存储在图片目录中。我需要逐一抓住所有这些,并上传到驱动器。

而且,我已经看到,如果给定的文件(使用相同的标题)已经存在,即使在当时,用相同的标题一个新的文件在驱动器中创建(显然有差异,但驱动器Id标题相同)。我想检查一下,如果文件存在,只上传,如果不存在,则跳过该文件。

请帮助..我一直在试图通过谷歌指在GitHub上的Android演示,但一直只能够做到使用的点点滴滴。

文件标题在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

万一有人正在寻找如何上传多个文件到驱动器,这里是解决方案,为我工作:

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); 
      } 
     }; 
} 

这不是完美的解决方案,只是一个工作代码。