1
votes

How to get list of file of specific folder from google drive using drive api

Ref :: https://developers.google.com/drive/android/deprecation

FileList

 public  Task<FileList> fetchLatestInformation(@NotNull String backupDir) {
        return Tasks.call(mExecutor, () -> {
            Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
            StringBuilder sb = new StringBuilder();
            sb.append("mimeType = 'application/vnd.google-apps.folder' and name = '");
            sb.append(backupDir);
            sb.append("'");
            list.setQ(sb.toString());
            list.setPageSize(1000);
            list.setOrderBy("modifiedTime");
            return list.execute();
        });
    }

ActivityCode

    googleDrive.fetchLatestInformation(backupDir)
            .addOnSuccessListener {
                var files = it.files
                files.reverse()
                files.forEach {
                    Log.e(TAG,"file :: ${it?.name}")
                }
                var file = files.firstOrNull()

                if (file != null) {
                    var time = file.modifiedTime
                    var date = Date(time.value)
                    var simpleDateFormat = SimpleDateFormat("EEE, dd MMM yyyy hh:mm aaa")
                    Log.e(TAG,"lastBackup name :: ${file.name}")
                    Log.e(TAG,"lastBackup date :: ${simpleDateFormat.format(date)}")
                    Log.e(TAG,"lastBackup size :: ${file.getSize()?.formatFileSize}")

                }
            }
            .addOnFailureListener {
                it.printStackTrace()
            }

LogResult

E/BackupActivity: file :: AppBackup
E/BackupActivity: lastBackup name :: AppBackup
E/BackupActivity: lastBackup date :: Tue, 19 May 2020 10:44 PM
    lastBackup size :: null

but AppBackup is directory which contains the backup files zips so my question is how to get a files which is stored in AppBackup Directory.

NOTE :: I AM USING DRIVE API V3

2

2 Answers

2
votes
public  Task<FileList> fetchLatestInformation(@NotNull String parentFolderId) {
        return Tasks.call(mExecutor, () -> {

            Drive.Files.List list = drive.files().list().setFields("nextPageToken, files(id, name, modifiedTime, size, createdTime, parents, appProperties)");
            StringBuilder sb = new StringBuilder();
            sb.append("'");
            sb.append(parentFolderId);
            sb.append("'");
            sb.append(" in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false");
            list.setQ(sb.toString());
            list.setPageSize(1000);
            list.setOrderBy("modifiedTime");
            return list.execute();
        });
    }

Children.List was removed in V3 so we want to use list() we can query using 'parentFolderId' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false

In v2, Children: list is used to list a folder's children and to list all children of the root folder, use the alias root for the folderId value.

However, it's stated in Migrate to Google Drive API v3 that Children and Parents collections have been removed. Use files.list instead.

Ref : How to search the folder in the google drive V3 using java?

1
votes

Use HTTP request GET https://www.googleapis.com/drive/v2/files/folderId/children to get children of folder using folder Id

import com.google.api.services.drive.Drive;
import com.google.api.services.drive.Drive.Children;
import com.google.api.services.drive.model.ChildList;
import com.google.api.services.drive.model.ChildReference;

import java.io.IOException;

// ...

public class MyClass {

  // ...

  /**
   * Print files belonging to a folder.
   *
   * @param service Drive API service instance.
   * @param folderId ID of the folder to print files from.
   */
  private static void printFilesInFolder(Drive service, String folderId)
      throws IOException {
    Children.List request = service.children().list(folderId);

    do {
      try {
        ChildList children = request.execute();

        for (ChildReference child : children.getItems()) {
          System.out.println("File Id: " + child.getId());
        }
        request.setPageToken(children.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);
  }

  // ...

}

For more check