简体   繁体   English

将DriveId转换为人类可读的路径

[英]Convert DriveId to human readable path

In my app, I'm coding an activity to backup the local database to Google Drive. 在我的应用中,我正在编写一项活动,以将本地数据库备份到Google云端硬盘。 I let the user pick the folder where to save the file using Drive's intentPicker and saving the result in my OnActivityResult : 我让用户选择使用Drive的intentPicker将文件保存到的文件夹,并将结果保存在我的OnActivityResult

@Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        switch (requestCode) {
            // REQUEST_CODE_PICKER
            case 2:
                intentPicker = null;

                if (resultCode == RESULT_OK) {
                    //Get the folder's drive id
                    DriveId mFolderDriveId = data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);

                    uploadToDrive(mFolderDriveId);
                }

After uploading the file I want to show the user where the file was saved. 上传文件后,我想向用户显示文件的保存位置。 So, I need to convert folder's DriveId to a human readable path like /drive/MyBackups/May . 因此, 我需要将文件夹的DriveId转换为/ drive / MyBackups / May之类的可读路径。

I've already tried driveId.toString but it only returns a string with unhelpful numbers and letters like DriveId:CASDFAD2Jmasdf==... . 我已经尝试过driveId.toString但是它只返回一个无用的数字和字母的字符串,例如DriveId:CASDFAD2Jmasdf==...

Working with the Drive API methods for folders, note these important keys given in Drive Rest API - Work with Folders : 使用文件夹的Drive API方法时,请注意Drive Rest API- 使用文件夹中给出的这些重要键:

  • A folder is a file with the MIME type application/vnd.google-apps.folder and with no extension. 文件夹是MIME类型为application/vnd.google-apps.folder且没有扩展名的文件。
  • You can use the alias root to refer to the root folder anywhere a file ID is provided 您可以使用别名root来指代提供文件ID的任何位置的根文件夹

With these given keys, you can add in your query the parent ID with reference to the file that you're looking for then you can follow the logic given in this SO post - What's the right way to find files by “full path” in Google Drive API v2 . 使用这些给定的键,您可以在查询中添加与要查找的文件有关的父ID,然后可以遵循此SO帖子中给出的逻辑- 通过“完整路径”查找文件的正确方法是什么Google Drive API v2

I hope it will work for you. 希望它对您有用。

As ago suggested, even if possible, showing the full path of a folder is not expected for an app with Drive FILE scope. 正如所说,即使有可能,显示出不期望一个文件夹的完整路径与驱动器文件范围内的应用程序。 So I solved it requesting metadata and showing just the title. 因此,我解决了该问题,请求元数据并仅显示标题。

    private void setBackupFolderTitle(DriveId id){
        id.asDriveFolder().getMetadata((mGoogleApiClient)).setResultCallback(
            new ResultCallback<DriveResource.MetadataResult>() {
                @Override
                public void onResult(DriveResource.MetadataResult result) {
                    if (!result.getStatus().isSuccess()) {
                        showErrorDialog();
                        return;
                    }
                    Metadata metadata = result.getMetadata();
                    // Set folder title in TextView
                    folderTextView.setText(metadata.getTitle());
                }
            }
        );
    }

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

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