简体   繁体   中英

Google Drive API - populating list of files in ListView

I am using the demo found here as a guide for my app:

https://github.com/googledrive/android-demos/tree/master/src/com/google/android/gms/drive/sample/demo

I am first using the PickFolderWithOpenerActivity.java to retrieve the Google Drive folder responseID. I want the user to first drill down and select a folder, and then all the files in that folder to be displayed (and to be selectable)

The OpenFileActivityBuilder opens ok in my app and I can then browse the folder structure, and then press the "Select" button in order to select that folder.

In PickFolderWithOpenerActivity.java, I am overwriting the EXISTING_FOLDER_ID with the responseID (this response ID matches the drive folder URL ie the part in bold: https://drive.google.com/?authuser=0#folders/ 0AwbfSlc3Y1XYUXpZU0Vac1JXY28 ):

So essentially, EXISTING_FOLDER_ID = 0AwbfSlc3Y1XYUXpZU0Vac1JXY28

DriveId driveId = (DriveId) data.getParcelableExtra(
                            OpenFileActivityBuilder.EXTRA_RESPONSE_DRIVE_ID);
    EXISTING_FOLDER_ID = driveId.getResourceId();

I then call the the "ListFilesInFolderActivity.java":

public class ListFilesInFolderActivity extends BaseActivity  {

private ListView mResultsListView;
private ResultsAdapter mResultsAdapter;
//private static DriveId sFolderId = DriveId.decodeFromString(EXISTING_FOLDER_ID); // this causes error

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

    showMessage("EXISTING_FOLDER_ID:" + EXISTING_FOLDER_ID);
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FOLDER_ID)
            .setResultCallback(idCallback);



}

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());
        showMessage("Result driveId is:  " + result.getDriveId());
        folder.listChildren(getGoogleApiClient())
                .setResultCallback(metadataResult);



    }
};

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

                showMessage("Successfully listed files.");
                Intent intent2 = new Intent(ListFilesInFolderActivity.this, ListFilesActivity.class);
                startActivity(intent2);
            }
        };

}

This completes ok as I receive the following message:

 showMessage("Successfully listed files.");

I then call the ListFilesInFolderActivity which I think is populating the ListView (when I say call, I am using a new intent to show the activity).

But the problem is that only the top level folders and files are listed in the ListView. Not the files from the selected folder.

My intention is to display all the files from that "picked" folder and make them selectable (multi select).

I tried using the "PickFileWithOpenerActivity" but it only lets you choose one file from google drive. I want to be able to select multiple files in a folder. (I take it there is no way to change that)

Could someone take a look at the sample code and let me know if there is anything I need to change to get the ListView to populate with all the files from the SELECTED folder, (and to make the list multi selectable)?

Should I not be using responseID at all? Should I only be using driveId?

Thanks

The problem is with the Scope.

Google Drive Android API provides only drive.file and drive.appfolder scope.

File scope means access only to those files which the user chooses from your application. The Drive.DriveApi.newOpenFileActivityBuilder() dialog will let the user browse through the whole drive but your application will be granted access to only those file or folder which the user specifically Select's.

Suppose you have following folder structure on your drive -

/root
  |
  +---books
      +---c++
          +---programming_gems.pdf
          +---c++_notes.docs

Now using Drive.DriveApi.newOpenFileActivityBuilder() you pick up programming_gems.pdf. Your app has now obtained access to that pdf. You can retrieve the file and its metadata. But your app won't be able to obtain access to its parent folder, c++ in this case. Why? because the user has not yet given access to c++ folder; only a specific file within that folder. This makes sense also; otherwise you can retrieve any file from that folder leaving your user worried.

Pick up c++ folder using the same Drive.DriveApi.newOpenFileActivityBuilder() . Now if your app tries to retrieve the parent folder of programming_gems.pdf, it is granted access.

Try one more thing now; try to list the contents of c++ folder itself. What wil be returned to your app? - only programming_gems.pdf. Why, because the user has not granted access to the other file, c++_notes.docs.

I hope am clear.

PS

By the way, to select between picking up files or folder, you have to set the mime type. See the following code for eg,

if (openFolder) {
    mime_type = new String[]{DriveFolder.MIME_TYPE};
} else {
    mime_type = new String[]{};
}

IntentSender intentSender = Drive.DriveApi
        .newOpenFileActivityBuilder()
        .setMimeType(mime_type)
        .build(getGoogleApiClient());

If this scheme of scopes don't match your requirements you can alternatively use the Google Drive REST APIs - Read on - https://stackoverflow.com/a/25871516/1363471

The problem is that this SDK only offers SCOPE_FILE access, and you cannot list all a users files with this type of access.

I am hoping they will provide more access in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM