使用google drive api列出文件(Android)

时间:2016-03-19 18:47:00

标签: android google-drive-android-api

我正在尝试显示我的谷歌硬盘中的文件列表,但不显示任何内容或仅显示“应用程序数据”。

此部分代码用于连接google api,似乎它的工作正常:

 private void connectGoogleDriveApi(){
    if (mGoogleApiClient == null) {
        // Create the API client and bind it to an instance variable.
        // We use this instance as the callback for connection and connection
        // failures.
        // Since no account name is passed, the user is prompted to choose.
        mGoogleApiClient = new GoogleApiClient.Builder(this.baseActivity)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addScope(Drive.SCOPE_APPFOLDER) 
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }
    // Connect the client. Once connected, the camera is launched.
    mGoogleApiClient.connect();
}

 @Override
public void onConnected(Bundle bundle) {

    Utils.saveInfo("GoogleApiClass", "API client connected.");
    try{
        //when connect find the files
        listAllFiles();
        showDriveList();

    }catch (Exception e){
        Utils.saveError("GoogleApiClass-->onConnected", e.getMessage(), e);
    }

}

然后我正在尝试恢复数据:

  private void listAllFiles() throws Exception{
    Utils.saveInfo("GoogleApiClass", "List all files from drive");

    String rootFolder= Drive.DriveApi.getRootFolder(this.mGoogleApiClient).getDriveId().toString();
    Drive.DriveApi.fetchDriveId(this.mGoogleApiClient, rootFolder)
            .setResultCallback(idCallback);

}

final private ResultCallback<DriveApi.DriveIdResult> idCallback = new ResultCallback<DriveApi.DriveIdResult>() {
    @Override
    public void onResult(DriveApi.DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
            Utils.saveInfo("GoogleApiClass-->idCallback","Cannot find DriveId. Are you authorized to view this file?");
            return;
        }
        DriveId driveId = result.getDriveId();
        DriveFolder folder = driveId.asDriveFolder();
        folder.listChildren(mGoogleApiClient)
                .setResultCallback(metadataResult);
    }
};

final private ResultCallback<DriveApi.MetadataBufferResult> metadataResult = new
        ResultCallback<DriveApi.MetadataBufferResult>() {
            @Override
            public void onResult(DriveApi.MetadataBufferResult result) {
                if (!result.getStatus().isSuccess()) {
                    Utils.saveInfo("GoogleApiClass-->idCallback","Problem while retrieving files");
                    return;
                }
                mResultsAdapter.clear();
                mResultsAdapter.append(result.getMetadataBuffer());
                Utils.saveInfo("GoogleApiClass-->idCallback","Successfully listed files.");
            }
        };

有什么想法吗?

提前致谢。

0 个答案:

没有答案