简体   繁体   English

如何使用Google Drive Android API打开特定文件?

[英]How to open a specific file using Google Drive Android API?

I am trying to use Google Drive Android API to read a file from Google drive, and I was trying to understand the example Google gives below. 我正在尝试使用Google Drive Android API从Google驱动器读取文件,并且试图理解Google在下面提供的示例。 What I am confused about is that what is Drveid and EXISTING_FILE_ID? 我感到困惑的是什么是Drveid和EXISTING_FILE_ID? How do I get them? 我如何获得它们? If I want to open a file by its name, how do I do that? 如果要按文件名打开文件,该怎么办? Thanks! 谢谢!

public class EditContentsActivity extends BaseDemoActivity {

private static final String TAG = "EditContentsActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);

    final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
        @Override
        public void onResult(DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }
            DriveId driveId = result.getDriveId();
            DriveFile file = driveId.asDriveFile();
            new EditContentsAsyncTask(EditContentsActivity.this).execute(file);

        }
    };
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
          .setResultCallback(idCallback);
}

public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {

    public EditContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected Boolean doInBackgroundConnected(DriveFile... args) {
        DriveFile file = args[0];
        try {
            DriveContentsResult driveContentsResult = file.open(
                    getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
            if (!driveContentsResult.getStatus().isSuccess()) {
                return false;
            }
            DriveContents driveContents = driveContentsResult.getDriveContents();
            OutputStream outputStream = driveContents.getOutputStream();
            outputStream.write("Hello world".getBytes());
            com.google.android.gms.common.api.Status status =
                    driveContents.commit(getGoogleApiClient(), null).await();
            return status.getStatus().isSuccess();
        } catch (IOException e) {
            Log.e(TAG, "IOException while appending to the output stream", e);
        }
        return false;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if (!result) {
            showMessage("Error while editing contents");
            return;
        }
        showMessage("Successfully edited contents");
    }
}
  • DriveId is defined here as: A canonical identifier for a Drive resource. DriveId 在这里定义为:Drive资源的规范标识符。 The identifier can be converted to a String representation for storage using encodeToString() and then later converted back to the ID representation using decodeFromString(String). 可以使用encodeToString()将标识符转换为String表示形式进行存储,然后再使用encodeFromString(String)将其转换回ID形式。 equals(Object) can be used to see if two different identifiers refer to the same resource. equals(Object)可用于查看两个不同的标识符是否引用同一资源。

  • EXISTING_FILE_ID in Google's drive api demo code refers to the saved file id of an existing file in drive. Google的驱动器API演示代码中的EXISTING_FILE_ID是指驱动器中现有文件的已保存文件ID。 file Id is auto-generated. 文件ID是自动生成的。

check this SO link for more on how to get driveId/file id to use that file 检查此SO链接以获取有关如何获取driveId /文件ID以使用该文件的更多信息

You can get a file by its name but its better to use driveId as its unique. 您可以通过文件名获得文件,但最好使用driveId作为其唯一文件。

Ok, here is how I did it. 好的,这就是我的做法。 FileID is auto-generated when file was created. 创建文件时会自动生成FileID。 Once we have fileId. 一旦我们有了fileId。 Below can be used to get the file. 下面可以用来获取文件。

    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), fileId).setResultCallback(idCallback);

Query can be used to get the FileId of files. 查询可用于获取文件的FileId。

    Query query = new Query.Builder().addFilter(Filters.eq(SearchableField.TITLE, fileName)).build();
    Drive.DriveApi.query(getGoogleApiClient(), query).setResultCallback(metadataCallback);

At least the codes above were what Google did in its demo app. 至少上面的代码是Google在其演示应用程序中所做的。

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

相关问题 如何使用Google Drive Android API删除Google Drive上的文件 - How to delete a file on google drive using Google Drive Android API 如何使用Android API和特定帐户连接到Google云端硬盘? - How to connect to Google Drive using the Android API and a specific account? 不使用Drive API以编程方式在Google云端硬盘中打开文件 - Open a file in Google Drive programmatically not using Drive API 如何使用 API 在 Google Drive 上创建文件夹和文件? 安卓 - How to create a folder and a file on Google Drive using API? Android 如何使用Google Drive Android API更改在Google云端硬盘中创建的文件夹或文件的权限? - How to change permission of folder or file created in Google Drive using Google Drive Android API? 如何使用 Google Drive API 从 android 将加密的图像文件上传到 Google Drive - How to upload encrypted image file on google drive from android using Google Drive API 使用适用于Android的Google Drive API打开文件| 内部错误 - Open file using Google Drive API's for Android | INTERNAL_ERROR Google Drive API - Android - 如何获取驱动器文件 ID? - Google Drive API - Android - How to obtain a drive file id? 如何使用Google Drive Android API获取Drive_FILE_ID - how to get Drive_FILE_ID using Google Drive android API 如何在Android上使用Google云端硬盘打开Goog​​le文档文件 - How can I open a google doc file with Google Drive on Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM