2015-06-27 150 views
1

在我的Andorid應用程序中,我只想訪問通過web(drive.google.com)或應用程序與我共享的文件。我使用谷歌驅動器API訪問共享文件,但我無法訪問與我共享的任何文件或文件夾。我的代碼如下。如何訪問共享文件

public class QueryFilesSharedWithMeActivity extends BaseDemoActivity { 

private ListView mResultsListView; 
private ResultsAdapter mResultsAdapter; 

@Override 
protected void onCreate(Bundle b) { 
    super.onCreate(b); 
    setContentView(R.layout.activity_listfiles); 
    mResultsListView = (ListView) findViewById(R.id.listViewResults); 
    mResultsAdapter = new ResultsAdapter(this); 
    mResultsListView.setAdapter(mResultsAdapter); 
} 

/** 
* Clears the result buffer to avoid memory leaks as soon as the activity is no longer 
* visible by the user. 
*/ 
@Override 
protected void onStop() { 
    super.onStop(); 
    mResultsAdapter.clear(); 
} 

@Override 
public void onConnected(Bundle connectionHint) { 
    super.onConnected(connectionHint); 
    Query query = new Query.Builder() 
      .addFilter(Filters.sharedWithMe()) 
      .build(); 
    Drive.DriveApi.query(getGoogleApiClient(), query) 
      .setResultCallback(metadataCallback); 
} 

final private ResultCallback<DriveApi.MetadataBufferResult> metadataCallback = 
     new ResultCallback<DriveApi.MetadataBufferResult>() { 
      @Override 
      public void onResult(DriveApi.MetadataBufferResult result) { 
       if (!result.getStatus().isSuccess()) { 
        showMessage("Problem while retrieving results"); 
        return; 
       } 
       mResultsAdapter.clear(); 
       mResultsAdapter.append(result.getMetadataBuffer()); 
      } 
     }; 

}

注*我們可以過濾與我分享了一個特定的文件夾名稱的文件或文件夾?

+0

你說「我無法訪問與我共享的任何文件或文件夾」。您的意思是GoogleDrive文件夾和文件是否由他人擁有,並與您分享? –

+0

@qbix是的,我想訪問谷歌驅動器文件夾和其他擁有,但與我共享的文件。 – Sirconrad

+0

[此相關問題]的答案(http://stackoverflow.com/questions/26929154/unable-to-get-the-shared-with-me-files-with-the-google-drive-api-using- filters-s?lq = 1)表示Android GoogleDrive API不支持訪問共享文件,即使存在查詢選項。當你運行你的查詢時,你會得到任何結果嗎? –

回答

0

您的應用有權訪問共享文件嗎? 目前Google Drive Android API僅支持FILE作用域訪問(請參閱詳細信息here)。因此,如果您的應用無權訪問這些共享文件,您將無法在查詢結果中收到這些文件。

+0

hello Shailendra,我的文件已經授權訪問本文件。目前它只訪問由我的應用程序創建的文件。我想訪問這些從web創建的文件。你可以請加我skype sachin.auxiliumit。我會非常感謝你。我從過去7天開始掙扎。 – Sirconrad

+0

如果在網絡上創建驅動器文件,用戶必須專門授權Drive文件上的FILE範圍應用程序,以便應用程序可以訪問它。訪問共享文件的基本原因是什麼?如果您想對單個文件執行操作,則可以請求[打開文件活動](https://developers.google.com/android/reference/com/google/android/gms/drive/OpenFileActivityBuilder),其中用戶可以選擇一個雲端硬盤文件(可以是共享文件),並且您的應用程序獲取所選文件的DriveId以對其執行任何操作。 – Shailendra