简体   繁体   English

使用Google Drive API - 如何在根文件夹中显示文件夹列表

[英]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时,身份验证似乎正常,但我不知道如何检索和显示云端硬盘文件夹。

  1. How can they be retrieved? 它们如何被检索?
  2. What should they be displayed in ( 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.

相关问题 如何在分页和使用 order by 时列出 Google Drive API v3 上的所有根文件夹和共享? - How to list all root folder and shares on the Google Drive API v3 while paging and using order by? Google云端硬盘API-获取文件夹列表 - Google Drive API - Get folders list Android Google Drive SDK - 如何显示所选文件夹中的文件列表 - Android Google Drive SDK - how to show list of files in selected folder 如何使用Google的REST v3 API在Google云端硬盘的根目录中列出所有文件,而忽略子目录? - How to list all files , ignoring subdirectories, in a Google Drive's root location using Google's REST v3 API? 无法使用新的Drive API检索Google云端硬盘文件和文件夹 - Unable to retrieve Google Drive files and folders using new Drive API 如何使用 API 在 Google Drive 上创建文件夹和文件? 安卓 - How to create a folder and a file on Google Drive using API? Android 如何在根目录中创建文件夹并在Android的Google Drive REST API v3中获取其ID - How to create a folder in root and get its ID in Google Drive REST API v3 in Android 如果使用google drive Api在android中的google drive中不存在,如何创建文件夹? - how to create folder if not exist in google drive in android using google drive Api? 如何使用Google Drive Android API更改在Google云端硬盘中创建的文件夹或文件的权限? - How to change permission of folder or file created in Google Drive using Google Drive Android API? 适用于Android的云端硬盘API:如何列出Google云端硬盘文件夹中的每个条目(以及不是由我的应用创建的条目) - Drive API for Android: how to list every entry in Google Drive folder (also those not created by my app)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM