简体   繁体   中英

Google Drive SDK for Android: How to get folder id if I know folder title

我需要将文件上传到先前由其他用户创建的Google云端硬盘上的文件夹中,我知道该文件夹的名称,如果我知道文件夹名称(标题),如何从android应用程序中以编程方式获取文件夹ID。

The Drive Android API currently only supports per file access, known as File scope. Users would need to select the folder via the file picker in order to authorize your app to access it.

We hope to be able to add Drive scope to the Android API in future, however I am not able to share a timeline at this stage. It would be great if you could share more details of your use case for this feature on our issue tracker .

In the interim you should be able to support your use case with the Java REST API .

As Daniel pointed out you have to use the REST Api in your case. To be more specific, here is a method that would do just that:

static com.google.api.services.drive.Drive mGOOSvc;

/**************************************************************************
 * find file/folder in GOODrive
 * @param prnId   parent ID (optional), null searches full drive, "root" searches Drive root
 * @param titl    file/folder name (optional)
 * @param mime    file/folder mime type (optional)
 * @return        arraylist of found objects
 */
static ArrayList<ContentValues> search(String prnId, String titl, String mime) {
  ArrayList<ContentValues> gfs = new ArrayList<>();
  if (mGOOSvc != null) try {
    // add query conditions, build query
    String qryClause = "'me' in owners and ";
    if (prnId != null) qryClause += "'" + prnId + "' in parents and ";
    if (titl != null) qryClause += "title = '" + titl + "' and ";
    if (mime != null) qryClause += "mimeType = '" + mime + "' and ";
    qryClause = qryClause.substring(0, qryClause.length() - " and ".length());
    Drive.Files.List qry = mGOOSvc.files().list().setQ(qryClause)
      .setFields("items(id,mimeType,labels/trashed,title),nextPageToken");
    String npTok = null;
    if (qry != null) do {
      FileList gLst = qry.execute();
      if (gLst != null) {
        for (File gFl : gLst.getItems()) {
          if (gFl.getLabels().getTrashed()) continue;
          gfs.add( UT.newCVs(gFl.getTitle(),gFl.getId()));
        }
        npTok = gLst.getNextPageToken();
        qry.setPageToken(npTok);
      }
    } while (npTok != null && npTok.length() > 0);
  } catch (Exception e) { /* handle the exceptions*/ }
  return gfs;
}

What you are after, is the gFl.getId() . You still have to get the com.google.api.services.drive.Drive mGOOSvc . For a full context, please see this Github demo

Good Luck

There are two questions in your post:

  1. How to upload a file in a folder created by some other user?

Ans. First of all you need to have the write permission on that folder in order to insert some content in that folder. If you have then make use of the CreateFileActivityBuilder class in order to create a file in a folder if you know the title and file and the destination folder. You can also use this class to specify the initial metadata and contents for the file.

Here is the code and detailed documentation :

ResultCallback<DriveContentsResult> contentsCallback = new
        ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            // Handle error
            return;
        }

        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                .setMimeType("text/html").build();
        IntentSender intentSender = Drive.DriveApi
                .newCreateFileActivityBuilder()
                .setInitialMetadata(metadataChangeSet)
                .setInitialDriveContents(result.getDriveContents())
                .build(getGoogleApiClient());
        try {
            startIntentSenderForResult(intentSender, 1, null, 0, 0, 0);
        } catch (SendIntentException e) {
            // Handle the exception
        }
    }
}
  1. Get the folderID of a folder whose name is known:

Ans. You need to atleast know the DriveID in order to retrieve the folderID of a particular folder. With the name of the folder you wont be able to retrieve folderID as there may be a lot of folders having same name but different ID's.

Here is the code and detailed documentation :

 private static DriveId sFolderId = DriveId.decodeFromString("DriveId:0B2EEtIjPUdX6MERsWlYxN3J6RU0");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM