2

Ich benutze Google Drive API, um einige Dateien im Google Drive App-Ordner zu speichern, aber auf einigen Geräten, wenn ich versuche, die Dateien herunterzuladen, gibt es einfach die Dateien mit der Größe von 0 Byte.Google Drive API für Android gibt leere Datei zurück

Upload-Code:

/** 
* 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; 
    } 
} 

Downloadcode:

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

Die hochgeladene Datei korrekt in Emulatoren und die meisten Geräte heruntergeladen, aber bei einigen Geräten ist es mit 0 Byte Dateigröße heruntergeladen.

Eigentlich, wenn ich eine Datei von einem Gerät mit diesem Problem hochladen und dann ich fordere es erneut mit dem gleichen Gerät herunterladen, erhalte ich eine Datei mit Nulllänge aus dem Cache, wenn ich versuche, diese Datei von einem anderen Gerät herunterzuladen hat dieses Problem es einfach herunterladen, ohne ein Problem.

Ich denke, etwas ist falsch mit seiner Caching-Strategie, es nur überprüfen, ob die Datei bereits im Cache vorhanden ist und gibt es mit 0 Byte Größe zurück.

Die Geräte, mit denen ich dieses Problem habe, sind api 21 und darunter.

Google Drive Api 9.2.1

Google Play-Dienste 9.4.52 Datum: 19. Juli 2016 Die neueste Version

Antwort

0

I gleichen Fehler haben. Und ich habe festgestellt, dass DriveId, das ich herunterladen möchte, eine 'vorläufige' DriveId ist (Folge dieser resourceID of file is null). Ich habe diese vorläufige DriveId in meiner Datenbank gespeichert und kann diese Datei nicht herunterladen. Ich behebe dies durch den Empfang von CompletionEvents beim Erstellen und Bearbeiten der Datei, um eine echte resourceID der Datei zu erhalten, wenn die GDAA-Commit-Datei an Drive (von receiving Completion Events) übergeben wird. Entschuldigung für mein schlechtes Englisch.

Verwandte Themen