Creating a folder inside a folder in google drive android

最后都变了- 提交于 2019-11-30 04:45:28

问题


I want to integrate Google drive with my app I have registered my app in Google Developers Console. I got a sample from https://github.com/googledrive/android-demos .By this i am able to create a file,folder in Google drive's root folder but the problem is I couldn't create a file or folder inside an existing folder. In such case i got a toast "Cannot find DriveId. Are you authorized to view this file?" ie I cannot get the driveID

public class CreateFolderInFolderActivity extends BaseDemoActivity {

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
            .setResultCallback(idCallback);
}

final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
    @Override
    public void onResult(DriveIdResult result) {
        if (!result.getStatus().isSuccess()) {
             showMessage(result.getStatus().toString());
            showMessage("Cannot find DriveId. Are you authorized to view this file?");
            return;
        }
        DriveFolder folder = Drive.DriveApi
                .getFolder(getGoogleApiClient(), result.getDriveId());
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle("MyNewFolder").build();
        folder.createFolder(getGoogleApiClient(), changeSet)
                .setResultCallback(createFolderCallback);
    }
};

final ResultCallback<DriveFolderResult> createFolderCallback = new
        ResultCallback<DriveFolderResult>() {

    @Override
    public void onResult(DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Problem while trying to create a folder");
            return;
        }
        showMessage("Folder successfully created");
    }
};
}

I cannot find any proper documentation for this. Plz help me where i am going wrong or Whether I Have to include any other permissions


回答1:


You can take a peek here: in the 'createTree()' method, there is a creation of folder within a folder.

There are 3 different drive ID entities in the new Google Drive Android API (GDAA)

  1. the object of type DriveID - the one you get from methods and use in your code
  2. a string you get from encodeToString() and pass to decodeFromString() - used to save within the app (caching for instance)
  3. a string you get from getResourceId() and pass to fetchDriveId() - the one you see in the html address of a file.

Both 2 and 3 identifiers are strings, so they may be confused. Identifier 2 is faster when retrieving Drive ID (via decodeFromString()). Identifier 3 is slower to retrieve (via fetchDriveId()), but usefull if you need to take your ID elsewhere (Apps Script, for instance).

Please see also: SO 21800257




回答2:


What is EXISTING_FOLDER_ID? If you are trying to just run the sample straight out without having made any changes, this won't work.

You need to change EXISTING_FOLDER_ID to the resource id of a folder that your app has access to. This could be a folder that your app created in the root.




回答3:


first create folder using creatreeTree()

then run a search query to get id of create public static ArrayList<ContentValues> search(String prnId, String titl, String mime) { ArrayList<ContentValues> gfs = new ArrayList<>(); if (mGOOSvc != null && mConnected) 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(), gFl.getMimeType())); } //else UT.lg("failed " + gFl.getTitle()); npTok = gLst.getNextPageToken(); qry.setPageToken(npTok); } } while (npTok != null && npTok.length() > 0); //UT.lg("found " + vlss.size()); } catch (Exception e) { UT.le(e); } return gfs; }

when you get folder id use this code to create folder in folder ` public static ParentReference insertFileIntoFolder(Drive service, String folderId, String folderName) throws IOException {

// Log.e("founddd",id); File fileMetadata = new File(); fileMetadata.setParents(Collections.singletonList(new ParentReference().setId(folderId == null ? "root" : folderId))); fileMetadata.setTitle(folderName); fileMetadata.setMimeType("application/vnd.google-apps.folder");

        File file  = mGOOSvc.files().insert(fileMetadata).execute();

        System.out.println("Folder ID: " + file.getId());
        strChildFolder = file.getId();




    return null;
}`


来源:https://stackoverflow.com/questions/22140319/creating-a-folder-inside-a-folder-in-google-drive-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!