2

我使用谷歌驱动器api存储在谷歌驱动器应用程序文件夹中的一些文件,但在一些设备上,当我尝试下载文件时,它只是返回大小为0字节的文件。谷歌驱动器API为Android返回空文件

上传代码:

/** 
* It's a blocking method 
* 
* @param file the file to upload to google drive. 
*/ 
private boolean uploadToDrive(@NonNull File file) { 
    final DriveApi.DriveContentsResult driveContentsResult = Drive.DriveApi 
      .newDriveContents(mGoogleApiClient) 
      .await(); 
    // If the operation was not successful, we cannot do anything and must fail. 
    if (!driveContentsResult.getStatus().isSuccess()) { 
     Logger.t(TAG).e("Failed to create new contents."); 
     return false; 
    } 
    // Otherwise, we can write our data to the new contents. 
    Logger.t(TAG).i("New empty contents created."); 

    //Creates a file in app folder with provided metadata. 
    final DriveFolder.DriveFileResult driveFileResult = Drive.DriveApi 
      .getAppFolder(mGoogleApiClient) 
      .createFile(mGoogleApiClient, getDatabaseMeta(file.getName().replace("temp-", "")), driveContentsResult.getDriveContents()) 
      .await(); 

    if (!driveContentsResult.getStatus().isSuccess()) { 
     Logger.t(TAG).e("Error while trying to create the file in app folder."); 
     return false; 
    } 

    final DriveApi.DriveContentsResult contentsResult = driveFileResult 
      .getDriveFile() 
      .open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null) 
      .await(); 

    if (!contentsResult.getStatus().isSuccess()) { 
     Logger.t(TAG).e("cant create a file in app folder"); 
     return false; 
    } 

    final DriveContents driveContents = contentsResult.getDriveContents(); 

    if (!writeFileToDrive(file, driveContents)) { 
     Logger.t(TAG).e("Cannot read or write to file"); 
     return false; 
    } 

    final Status status = driveContents.commit(mGoogleApiClient, null).await(); 

    if (!status.getStatus().isSuccess()) { 
     Logger.t(TAG).e("Cannot upload the file to drive"); 
     return false; 
    } 
    // TODO: 2016-01-19 Store this to use this this file later. 
    Logger.t(TAG).e("getDriveId:" + driveFileResult.getDriveFile().getDriveId().encodeToString()); 
    return true; 
} 
/** 
* Write the source file to destination drive contents file. 
* 
* @param file   the source {@link File} to read from. 
* @param driveContents the destination {@link DriveContents} to write to. 
*/ 
private boolean writeFileToDrive(File file, DriveContents driveContents) { 
    try { 
     FileInputStream is = new FileInputStream(file); 
     BufferedInputStream in = new BufferedInputStream(is); 
     byte[] buffer = new byte[8 * 1024]; 

     BufferedOutputStream out = new BufferedOutputStream(driveContents.getOutputStream()); 
     int n; 
     while ((n = in.read(buffer)) > 0) { 
      out.write(buffer, 0, n); 
     } 
     in.close(); 
     is.close(); 
     out.close(); 
     return true; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

下载代码:

@Nullable 
private DriveContents downloadFileFromDrive(@NonNull DriveId driveId) { 

    final DriveApi.DriveContentsResult driveContentsResult = driveId.asDriveFile().open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, new DriveFile.DownloadProgressListener() { 
     @Override 
     public void onProgress(long bytesDownloaded, long bytesExpected) { 
      Log.d(TAG, "onProgress() called with: bytesDownloaded = [" + bytesDownloaded + "], bytesExpected = [" + bytesExpected + "]"); 
     } 
    }).await(); 
    if (!driveContentsResult.getStatus().isSuccess()) { 
     Logger.t(TAG).e("Cannot download the file"); 
     return null; 
    } 
    return driveContentsResult.getDriveContents(); 
} 

/** 
* Writes the drive contents to the destination file. 
* 
* @param source 
* @param destination 
* @return true if the write is successful. 
*/ 
private boolean writeFileToDisk(@NonNull DriveContents source, @NonNull File destination) { 
    try { 
     final InputStream in = source.getInputStream(); 
     final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(destination)); 
     byte[] buffer = new byte[8 * 1024]; 

     int n; 
     while ((n = in.read(buffer)) > 0) { 
      out.write(buffer, 0, n); 
     } 
     out.close(); 
     in.close(); 
     return true; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 
    } 
} 

上传的文件是在仿真器和大多数设备正确下载,但在某些设备上下载0字节的文件大小。

实际上,当我从设备上传文件并解决此问题,然后再次请求使用同一设备下载文件时,如果我尝试从另一个设备下载该文件,有这个问题,只是下载它没有问题。

我认为它的缓存策略有些问题,它只是检查文件是否已经存在于缓存中并以0字节大小返回。

我有这个问题的设备是api 21及以下。

谷歌云端硬盘API 9.2.1

谷歌Play服务,52年4月9日日期:2016年7月19日最新版本

回答

0

我有同样的错误。我发现,我想下载的DriveId是'初步的'DriveId(按照这个resourceID of file is null)。我将“初步的”DriveId保存到我的数据库,并且不能下载该文件。 我通过在创建和编辑文件时接收CompletionEvents来解决此问题,以便在GDAA将文件提交到驱动器时​​获取文件的实际resourceID(由receiving Completion Events)。对不起,我的英语不好。

相关问题