[英]Using Google Drive API - how to show a list of folders in root folder
I am trying to create an Android App so that the user can browse and select files from their Google Drive. 我正在尝试创建Android应用,以便用户可以浏览并选择其Google云端硬盘中的文件。
I am totally new to Android and I have been mostly trying to use the samples. 我是Android新手,我一直在尝试使用这些示例。 When I run the APK on my phone, the authentication seems to work ok, but I am not sure how to retrieve and display the Drive folders. 当我在手机上运行APK时,身份验证似乎正常,但我不知道如何检索和显示云端硬盘文件夹。
ListView
?) so that users can navigate to the files? 应该在( ListView
?)中显示什么,以便用户可以导航到文件? My onConnected code is working ok, but I think the problem is to do with the DriveId not being populated correctly. 我的onConnected代码工作正常,但我认为问题是没有正确填充DriveId。
@Override
public void onConnected(Bundle connectionHint) {
Log.i(TAG, "API client connected.");
super.onCreate(connectionHint);
setContentView(R.layout.activity_listfiles);
mResultsListView = (ListView) findViewById(R.id.listViewResults);
mResultsAdapter = new ResultsAdapter(this);
mResultsListView.setAdapter(mResultsAdapter);
Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
.setResultCallback(idCallback);
}
I have a layout called "activity_listfiles.xml". 我有一个名为“activity_listfiles.xml”的布局。 On that layout is it just a ListView called: @id/listViewResults 在那个布局上它只是一个名为:@id / listViewResults的ListView
The code is going into this method as I am getting the DriveID error message: Cannot find DriveId. 当我收到DriveID错误消息时,代码将进入此方法:无法找到DriveId。 Are you authorized to view this file? 您是否有权查看此文件?
final private 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;
}
DriveFolder folder = Drive.DriveApi.getFolder(getGoogleApiClient(), result.getDriveId());
folder.listChildren(getGoogleApiClient())
.setResultCallback(metadataResult);
}
};
Can anyone point me in the right direction? 谁能指出我正确的方向? I am using the following as a guide: https://github.com/googledrive/android-demos/tree/master/src/com/google/android/gms/drive/sample/demo 我使用以下内容作为指南: https : //github.com/googledrive/android-demos/tree/master/src/com/google/android/gms/drive/sample/demo
Here is the ResultsAdapter java file, I'm not sure what it does or if I need to change it: 这是ResultsAdapter java文件,我不确定它是做什么的,或者我是否需要更改它:
public class ResultsAdapter extends DataBufferAdapter<Metadata> {
public ResultsAdapter(Context context) {
super(context, android.R.layout.simple_list_item_1);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(getContext(),
android.R.layout.simple_list_item_1, null);
}
Metadata metadata = getItem(position);
TextView titleTextView =
(TextView) convertView.findViewById(android.R.id.text1);
titleTextView.setText(metadata.getTitle());
return convertView;
}
}
I should add that the String EXISTING_FOLDER_ID is in a BaseActivity.java, and the line of code looks like: 我应该补充一点,字符串EXISTING_FOLDER_ID位于BaseActivity.java中,代码行如下所示:
public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0";
I take it I need to populate this with an ID somehow? 我认为我需要以某种方式使用ID来填充它?
I tried using: 我试过用:
EXISTING_FOLDER_ID = Drive.DriveApi.getRootFolder(getGoogleApiClient()).getDriveId().toString();
showMessage(EXISTING_FOLDER_ID);
Drive.DriveApi.fetchDriveId(getGoogleApiClient(),EXISTING_FOLDER_ID)
.setResultCallback(idCallback);
The showMessage DOES display an ID - so it IS fetching the root folder ID, but for some reason it still shows the "Cannot find DriveID" message. showMessage会显示一个ID - 因此它正在获取根文件夹ID,但由于某种原因它仍然显示“无法找到DriveID”消息。
So the fetchDriveID is failing? 那么fetchDriveID失败了吗?
Please see the need of the README here https://github.com/googledrive/android-demos/blob/master/README.md 请在此处查看README的需求https://github.com/googledrive/android-demos/blob/master/README.md
You need to change EXISTING_FOLDER_ID to point to a the resource id of a Drive folder that your application has access to. 您需要更改EXISTING_FOLDER_ID以指向您的应用程序有权访问的Drive文件夹的资源ID。 For example, you could create a new folder and then use that ID. 例如,您可以创建一个新文件夹,然后使用该ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.