簡體   English   中英

Google Drive API - Android - 如何獲取驅動器文件 ID?

[英]Google Drive API - Android - How to obtain a drive file id?

我正在嘗試開發一個可以讀取存儲在我的谷歌驅動器文件夾中的 xml 文件的 android 應用程序,最初的想法是嘗試打開文件並處理內容。

我已經閱讀了適用於 androidGoogle Drive API 文檔,但我已經迷失了,它正在處理文件內容。

根據本指南,從驅動器打開文件的方法是:

DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

搜索我意識到完整的代碼(它們不包括在內):

DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient,DriveId.bg(id));
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null).setResultCallback(contentsOpenedCallback);`

那么問題是我不知道文件“id”。 我已經嘗試過谷歌驅動器的網絡鏈接中的 id,類似這樣( https://drive.google.com/open ? id=1EafJ-T6H4xI9VaUuUO5FMVb9Y30xyr7OHuISQ53avso &authuser=0),但沒有用。

您可以使用 DriveAPI Query 方法來檢索有關特定文件的任何信息。 您需要定義一個查詢對象,如下所示:

Query query = new Query.Builder()
    .addFilter(Filters.eq(SearchableField.TITLE, "HelloWorld.java"))
    .build();

並設置一個回調函數來迭代結果:

Drive.DriveApi.query(googleApiClient, query)
        .setResultCallback(new OnChildrenRetrievedCallback() {

    @Override
    public void onChildrenRetrieved(MetadataBufferResult result) {
        // Iterate over the matching Metadata instances in mdResultSet
    }
});

您可以在此處找到有關該主題的更多信息: https : //developers.google.com/drive/android/queries

我為這個問題找到的解決方案是從應用程序創建文件。 使用 google drive api 演示應用程序中的類( “CreateFileActivity.java” )。

通過這個類,我將新文件中返回的 Driveid 保存在全局 DriveId 變量中。

final private ResultCallback<DriveFolder.DriveFileResult> fileCallback = new
        ResultCallback<DriveFolder.DriveFileResult>() {
            @Override
            public void onResult(DriveFolder.DriveFileResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("","Error while trying to create the file");
                    return;
                }
                Id=result.getDriveFile().getDriveId();
                Log.e("","Created a file with content: " + Id);

            }
        };

然后在另一種方法中使用此 id 調用該文件並讀取它(如果我願意,我可以從 Google Drive Web App 編輯此文件信息):

 public void leer(){
       DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),Id);
       file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
               .setResultCallback(contentsOpenedCallback);
   }
ResultCallback<DriveApi.DriveContentsResult> contentsOpenedCallback =
        new ResultCallback<DriveApi.DriveContentsResult>() {
            @Override
            public void onResult(DriveApi.DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    Log.e("Error:","No se puede abrir el archivo o no se encuentra");
                    return;
                }
                // DriveContents object contains pointers
                // to the actual byte stream
                DriveContents contents = result.getDriveContents();
                BufferedReader reader = new BufferedReader(new InputStreamReader(contents.getInputStream()));
                StringBuilder builder = new StringBuilder();
                String line;
                try {
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                String contentsAsString = builder.toString();
                Log.e("RESULT:",contentsAsString);
            }
        };

幾個月前我一直在玩這個東西,並且仍然在 github 上有一些代碼。 它可能非常過時(libver 15 左右),但它可以作為參考點,而且確實如此(我希望很簡單)。 看這里 將其拉下,插入,通過。 修復不再起作用的東西:-)。 我前段時間放棄了。

請注意,Google Drive Android API 對象有 2 個不同的 ID,請參閱SO 22841237

通常,您通常從知道文件/文件夾名稱開始,查詢 GDAA 以獲取對象列表。 它們中的每一個都會產生 DriveID 和 ResourceID。 DriveID 在您的應用程序中用於操作對象(並不意味着您的 Android 應用程序和/或設備之外的任何內容)。 ResourceID 是在 URL 中以不同形式出現的字符串,可以在您的應用程序之外使用(例如 Web 瀏覽器...)。 看看這個包裝器來感受一下它是如何工作的。 但同樣,它已經有幾個版本了,所以沒有保證。 祝你好運...

不推薦使用 Google Drive API,現在它的 Google Drive V3 和我們使用的查詢

String pageToken = null;
  do {
  FileList result = driveService.files().list()
  .setQ("mimeType='image/jpeg'")
  .setSpaces("drive")
  .setFields("nextPageToken, files(id, name)")
  .setPageToken(pageToken)
  .execute();
  for (File file : result.getFiles()) {
  System.out.printf("Found file: %s (%s)\n",
    file.getName(), file.getId());
  }
  pageToken = result.getNextPageToken();
  } 

  while (pageToken != null);

您可以在此處了解更多信息官方文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM