简体   繁体   中英

How to Create a Folder in App Folder in google drive in Android Programmatically

I am storing data in Google Drive using Google Drive API in Android programmatically. I referred to this github . But it is for create a folder in Root folder not in App Folder. Is it possible to create folder in AppFolder ? I tried some but no result my tried code.

public class CreateFolderInAppFolder extends BaseDemoActivity {

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
      .setTitle("New folder").build();
    DriveApi.getAppFolder(getGoogleApiClient()).createFolder(
            getGoogleApiClient(), changeSet).setResultCallback(callback);
}

final ResultCallback<DriveFolderResult> callback = new ResultCallback<DriveFolderResult>() {
    @Override
    public void onResult(DriveFolderResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create the folder");
            return;
         }
        showMessage("Created a folder: " + result.getDriveFolder().getDriveId());
      }
   };
}

Yes it is possible, try reading Storing Application Data :

The App Folder is a special folder that is only accessible by your application. Its content is hidden from the user and from other apps. Despite being hidden from the user, the App Folder is stored on the user's Drive and therefore uses the user's Drive storage quota. The App Folder can be used to store configuration files, temporary files, or any other types of files that belong to the user but should not be tampered with.

Create a file in the App Folder

Creating a file in the App Folder is the same as for a normal folder . The following example demonstrates creating a new text file named appconfig.txt in the App Folder:

Here is a sample code:

final private ResultCallback<DriveContentsResult> contentsCallback =
        new ResultCallback<DriveContentsResult>() {
    @Override
    public void onResult(DriveContentsResult result) {
        if (!result.getStatus().isSuccess()) {
            showMessage("Error while trying to create new file contents");
            return;
        }

        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle("appconfig.txt")
                .setMimeType("text/plain")
                .build();
        Drive.DriveApi.getAppFolder(getGoogleApiClient())
                .createFile(getGoogleApiClient(), changeSet, result.getDriveContents())
                .setResultCallback(fileCallback);
    }
};

You can check if the file/folder is in AppFolder by retreiving its metadata . The Metadata.isInAppFolder method returns true if the file or folder is in the App Folder.

Hope this helps.

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