user3437721
user3437721

Reputation: 2279

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.

I am totally new to Android and I have been mostly trying to use the samples. 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.

  1. How can they be retrieved?
  2. What should they be displayed in (ListView?) so that users can navigate to the files?

My onConnected code is working ok, but I think the problem is to do with the DriveId not being populated correctly.

@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". On that layout is it just a ListView called: @id/listViewResults

The code is going into this method as I am getting the DriveID error message: Cannot find 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

Here is the ResultsAdapter java file, I'm not sure what it does or if I need to change it:

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:

public static final String EXISTING_FOLDER_ID = "0B2EEtIjPUdX6MERsWlYxN3J6RU0";

I take it I need to populate this with an ID somehow?

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.

So the fetchDriveID is failing?

Upvotes: 4

Views: 6732

Answers (1)

Cheryl Simon
Cheryl Simon

Reputation: 46844

Please see the need of the README here 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. For example, you could create a new folder and then use that ID.

Upvotes: 2

Related Questions