2016-12-24 75 views
2

我正在使用Google Drive Api。我想從Google驅動器下載圖像。 下面是正在工作的代碼,但是當循環結束時調用files.open()。假設count是3,循環將首先完成循環,然後files.open()將被調用3次。正因爲如此,我無法在我的模型類中設置數據。它只需要最後一個值。 (編者)循環中的結果回調(從Google驅動器下載多個圖像。)

if (getGoogleApiClient() != null && getGoogleApiClient().isConnected()) { 
     try { 
      final DriveFolder pFldr = Drive.DriveApi.getRootFolder(getGoogleApiClient()); 
      pFldr.listChildren(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() { 
       @Override 
       public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) { 
        int count = metadataBufferResult.getMetadataBuffer().getCount(); 
        Log.e("COUNT---", count + ""); 
        MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer(); 
        if (count > 0) { 
         for (int i = 0; i < count; i++) { 
          if (buffer.get(i).getMimeType().equals("image/jpeg")) { 
           Log.e("Images avaialbe - ", "" + buffer.get(i).getTitle()); 
           DriveId id = buffer.get(i).getDriveId(); 
           DriveFile files = Drive.DriveApi.getFile(getGoogleApiClient(), id); 
           driveModel.setTitle(buffer.get(i).getTitle()); 

           files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(saveImageCallback); 
          } 
          if (buffer.get(i).getMimeType().equals("application/x-sqlite3")) { 
           DriveId id = buffer.get(i).getDriveId(); 
           DriveFile files = Drive.DriveApi.getFile(getGoogleApiClient(), id); 
           files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null).setResultCallback(saveDataBaseCallback); 
          } 
         } 

         buffer.release(); 
         showMessage("Success"); 
        } else { 
         showMessage("No Back Up available."); 
        } 
       } 
      }); 

     } finally { 

    } 

} 

     ResultCallback<DriveApi.DriveContentsResult> saveImageCallback = new ResultCallback<DriveApi.DriveContentsResult>() { 
      @Override 
      public void onResult(DriveApi.DriveContentsResult driveContentsResult) { 

       DriveContents cont = driveContentsResult.getDriveContents(); 
       InputStream inputStream = cont.getInputStream(); 
       try { 

        saveFiles(inputStream, driveModel.getTitle(), false); 
        cont.discard(getGoogleApiClient()); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 
+0

如果是第一次捕捉'try-catch'? –

+0

@GürtuğGüngör,這是一個嘗試,最後,沒有趕上。 –

+0

@ OleV.V。,對不起,我編輯時誤刪了,然後當我看着我的編輯版本,我沒有看到它,並寫下了問題。 –

回答

1

試試這個: 1)更改爲ArrayList<DriveModel> driveFileList; 2)

pFldr.listChildren 
       (getGoogleApiClient()) 
       .setResultCallback(new ResultCallback<DriveApi.MetadataBufferResult>() { 
       @Override 
       public void onResult(DriveApi.MetadataBufferResult metadataBufferResult) { 
       int count = metadataBufferResult.getMetadataBuffer().getCount(); 
       Log.e("COUNT---", count + ""); 
       Log.e("CHECk" , "Calling callback read from googledrive"); 
       MetadataBuffer buffer = metadataBufferResult.getMetadataBuffer(); 
       if (count > 0) { 
        for (int i = 0; i < count; i++) { 
        final int finalI = i; 

        Log.e("CHECk" , "reading file " + buffer.get(i).getTitle()); 
        Log.e("Files avaialbe - ", "" + buffer.get(i).getTitle()); 
        driveModel=new DriveModel(); 
        DriveId id = buffer.get(i).getDriveId(); 
        DriveFile files = Drive.DriveApi.getFile(getGoogleApiClient(), id); 

        if (buffer.get(i).getMimeType().equals("image/jpeg") || 
          buffer.get(i).getMimeType().equals("image/png")) { 

          driveModel.setTitle(buffer.get(i).getTitle()); 
          Log.e("Images avaialbe - ", "" + buffer.get(i).getTitle()); 
          // driveModel.setTitle(buffer.get(i).getTitle()); 


        files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null) 
         .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
           @Override 
           public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) { 
               saveData(driveContentsResult, finalI); 
            } 
           }); 
             driveFileList.add(driveModel); 

             } 

        if (buffer.get(i).getMimeType().equals("application/x-sqlite3")) { 

          files.open(getGoogleApiClient(), DriveFile.MODE_READ_ONLY, null) 
            .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() { 
             @Override 
             public void onResult(@NonNull DriveApi.DriveContentsResult driveContentsResult) { 
             saveData(driveContentsResult, finalI); 
           } 
          }); 

          } 

         } 

           buffer.release(); 
           showMessage("Success"); 
        } else { 
           showMessage("No Back Up available."); 
          } 

          } 
       } 

      ); 

3)創建方法:

private void saveData(DriveApi.DriveContentsResult driveContentsResult, int i) { 
      Log.e("CHECk" , "Calling callback " + driveContentsResult.getDriveContents().getDriveId().encodeToString()); 
      DriveContents cont = driveContentsResult.getDriveContents(); 

      Log.e("File name :" , "File : " + driveFileList.get(i).getTitle()); 

//Put your code here 
      InputStream inputStream = cont.getInputStream(); 
      try { 

       saveFiles(inputStream, true); 
       cont.discard(getGoogleApiClient()); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
+0

driveFileList.get(i).getTitle();上的java.lang.IndexOutOfBoundsException; –

+0

你會缺少driveFileList.add(driveModel);在你的if條件..我沒有嘗試application/x-sqlite3語句。 –

+0

把一些驗證,如果你不想添加「application/x-sqlite3」的文件名 –