我想从谷歌驱动器列出文件和文件夹,我需要使用此代码,但我不知道如何初始化Drive类,以便我可以执行此功能。请帮我!
private static List<File> retrieveAllFiles(Drive service) throws IOException {
List<File> result = new ArrayList<File>();
Files.List request = service.files().list();
do {
try {
FileList files = request.execute();
result.addAll(files.getItems());
request.setPageToken(files.getNextPageToken());
} catch (IOException e) {
System.out.println("An error occurred: " + e);
request.setPageToken(null);
}
} while (request.getPageToken() != null &&
request.getPageToken().length() > 0);
return result;
}
答案 0 :(得分:0)
您可以参考此链接
https://software.intel.com/en-us/android/articles/connecting-to-google-drive-from-an-android-app
下载示例源代码并进行调试 您可以在以下位置触发该行:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Connect to Google Drive
mCredential = GoogleAccountCredential.usingOAuth2(this, Arrays.asList(DriveScopes.DRIVE));
startActivityForResult(mCredential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
//bla... bla...
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
{
switch (requestCode)
{
case REQUEST_ACCOUNT_PICKER:
if(resultCode == RESULT_OK && data != null && data.getExtras() != null)
{
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if(accountName != null)
{
mCredential.setSelectedAccountName(accountName);
mService = getDriveService(mCredential);
}
}
break;
}
}