循环结果回调(从谷歌驱动器下载多个图像。)

时间:2016-12-24 09:00:34

标签: java android callback google-drive-android-api

我正在使用Google Drive Api。我想从Google云端硬盘下载图片。 下面是正在运行的代码,但是在循环结束时调用files.open()。假设计数为3,循环将首先完成循环,之后将调用files.open()3次。因此,我无法在我的模型类中设置数据。它只需要最后一个值。 (编者)

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

1 个答案:

答案 0 :(得分: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();
            }
        }