简体   繁体   English

如何以编程方式在Google驱动器中的Google驱动器的App文件夹中创建文件夹

[英]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. 我正在使用Android中的Google Drive API将数据存储在Google Drive中。 I referred to this github . 我提到了这个github But it is for create a folder in Root folder not in App Folder. 但这是为了在根文件夹而不是App文件夹中创建一个文件夹。 Is it possible to create folder in AppFolder ? 是否可以在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: 下面的示例演示在“应用程序文件夹”中创建一个名为appconfig.txt的新文本文件:

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 . 您可以通过获取元数据来检查文件/文件夹是否在AppFolder中。 The Metadata.isInAppFolder method returns true if the file or folder is in the App Folder. 如果文件或文件夹位于“应用程序文件夹”中,则Metadata.isInAppFolder方法将返回true

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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