有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何以编程方式在Android中的google drive中创建应用程序文件夹中的文件夹

我使用Android中的GoogleDrive API以编程方式将数据存储在GoogleDrive中。我提到这一点。但它用于在根文件夹中创建文件夹,而不是在应用程序文件夹中创建文件夹。是否可以在AppFolder中创建文件夹? 我尝试了一些,但没有结果我尝试的代码

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());
      }
   };
}

共 (1) 个答案

  1. # 1 楼答案

    是的,这是可能的,试着阅读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.

    在App文件夹中创建一个文件

    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:

    下面是一个示例代码:

    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);
        }
    };
    

    您可以通过检索其metadata来检查文件/文件夹是否在AppFolder中。如果文件或文件夹在App文件夹中,^{}方法返回true

    希望这有帮助