如何使用 Google 驱动器 Android API 打开特定文件?

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

我正在尝试使用 Google 驱动器 Android API 从 Google 驱动器读取文件,我试图理解示例 Google 下面给出。我感到困惑的是什么是 Drveid 和 EXISTING_FILE_ID?我如何得到它们?如果我想按文件名打开文件,我该怎么做?谢谢!

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 定义 here 为:Drive 资源的规范标识符。可以使用 encodeToString() 将标识符转换为 String 表示形式进行存储,然后使用 decodeFromString(String) 将其转换回 ID 表示形式。 equals(Object) 可用于查看两个不同的标识符是否引用同一资源。

  • EXISTING_FILE_ID in Google's drive api demo code refer to the saved file id of an existing驱动器中的文件。文件 ID 是自动生成的。

查看此 SO link 以了解有关如何获取 driveId/file id 以使用该文件的更多信息

您可以通过文件名获取文件,但最好使用 driveId 作为其唯一性。

好的,这就是我的做法。文件 ID 是在创建文件时自动生成的。一旦我们有了 fileId。下面可以用来获取文件。

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

查询可用于获取文件的FileId。

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

至少上面的代码是 Google 在其演示应用程序中所做的。