Search code examples
androidgoogle-drive-apigoogle-drive-android-api

How to open content from a DriveFolder (Api Google-Drive)?


I have asked this question because I have a problem to open the contents of a folder that is in Google, I want to be able to open its contents to get the data and copy them into another folder. But the method I use gives me error I do not know if this method is only for files.

My current code only opens the contents of files as I already comment and I an error here this:

private void readFile(DriveId fileDriveId) {

DriveFile file = fileDriveId.asDriveFile();

file.open(apiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
    @Override
    public void onResult(DriveApi.DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            Log.e(LOGTAG,"Error");
            return;
        }

        DriveContents contents = result.getDriveContents();

        BufferedReader reader =
            new BufferedReader(
                new InputStreamReader(contents.getInputStream()));

        StringBuilder builder = new StringBuilder();

        try {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } catch (IOException e) {
            Log.e(LOGTAG,"Error");
        }

        contents.discard(apiClient);

        Log.i(LOGTAG, "YES": " + builder.toString());
    }

}); }

If they notice where they fail or know if there is another way to achieve the goal. Please comment.


Solution

  • If you have a problem in getting the contents of a folder in Drive API, then you can try to check this Search a folder's contents. Also, it is stated here, that folder contents can be queried using [DriveFolder.queryChildren](https://developers.google.com/android/reference/com/google/android/gms/drive/DriveFolder#queryChildren(com.google.android.gms.common.api.GoogleApiClient, com.google.android.gms.drive.query.Query)).

    For more information, you can check this documentation on how to work with File Contents.