0

I want to retrieve metadata from files or folders in Google Drive on Android device in order to get deviceID or resourceID, so then I could download the file to local storage of the device. The problem with my application is that, the application doesn't need user interaction. It mean that, just tell the application the name so then the app will try to find the files in every directory and download it. I have try to use query (link) follow quickstart tutorial but it return me only the name of the existing file only.

Note again: User does not need to select files or folders, just tell the name of the file is enough. And the file or folder is created by the application too.

Nara Na
  • 101
  • 1
  • 2
  • 10
  • Does meta data of a file contain a deviceId? Please explain. – greenapps Oct 23 '16 at 09:34
  • Also explain why you would need a deviceId to download a file. – greenapps Oct 23 '16 at 09:35
  • @greenapps, i am not so sure. i read info in https://developers.google.com/drive/android/metadata and they said that it contain all details about a file or folder. So doesn't it mean that it include deviceID too? Why i need deviceID to download a file? because i saw many samples shown that when we need to retrieve file from google drive, we need to have deviceID or resourceID. I am not sure mostly about all of this because i could not find enough example out there and mostly they are too hard to follow. – Nara Na Oct 23 '16 at 10:30
  • `it contain all details about a file or folder. So doesn't it mean that it include deviceID too?`. Well i dont know so asked you to explain it. I hope you will find out. Good luck. – greenapps Oct 23 '16 at 10:34
  • @greenapps, do you know how to find deviceid or resourceId then? it took many weeks now but i still could not find any answer yet. just retrieve deviceid or resourceid in the background without user interaction. so have you had any experience about it? – Nara Na Oct 23 '16 at 10:42

1 Answers1

0

How to get metadata from files or folders in Google Drive on Android?

According to Working with File and Folder Metadata:

Metadata is encapsulated in the Metadata class and contains all details about a file or folder including the title, the MIME type, and whether the file is editable, starred or trashed. The metadata is fetched for a DriveResource by calling the DriveResource.getMetadata method.

Here's a snippet from the docs:

/**
 * An activity to retrieve the metadata of a file.
 */
public class RetrieveMetadataActivity extends BaseDemoActivity implements
        ResultCallback {

    @Override
    public void onConnected(Bundle connectionHint) {
        DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(),
                DriveId.decodeFromString("0ByfSjdPVs9MZcEE3bzJCc3NsRkE"));
        file.getMetadata(getGoogleApiClient()).setResultCallback(metadataRetrievedCallback);

    }

    ResultCallback<MetadataResult> metadataRetrievedCallback = new
            ResultCallback<MetadataResult>() {
        @Override
        public void onResult(MetadataResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Problem while trying to fetch metadata");
                return;
            }
            Metadata metadata = result.getMetadata();
            showMessage("Metadata succesfully fetched. Title: " + metadata.getTitle());
        }
    }
}

How to get resource ID?

Still on the Android API for Drive, DriveId class has a method getResourceId () which returns the resource ID.

How to get device ID?

I don't think you can use the Android Drive API to get this. It seems you've mistaken this for fileID which you'll be using to Download Files.

Locate fileID manually in Google Drive:

If it's a spreadSheet file

https://docs.google.com/spreadsheets/d/1pE9ejBTBH38oCoOHU2O42qU6vzxagAJ9J1237dYB1Eg/edit#gid=0

fileID -> 1pE9ejBTBH38oCoOHU2O42qU6vzxagAJ9J1237dYB1Eg

If it's a doc file:

   https://docs.google.com/document/d/1Fh6s7an-7I6VuDBxZKcxcaU3cG1XpSryHQXGnznWlns/edit

fileID -> 1Fh6s7an-7I6VuDBxZKcxcaU3cG1XpSryHQXGnznWlns

You get the pattern. It's a string of alphanumeric characters in the URL.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Hi @noogui! Could you explain me below code? Mostly I don't understand where can I get this string "0ByfSjdPVs9MZcEE3bzJCc3NsRkE". Does it identify any file in Google Drive? If so is each file is the same? And how can I get this string too? DriveFile file = Drive.DriveApi.getFile(getGoogleApiClient(), DriveId.decodeFromString("0ByfSjdPVs9MZcEE3bzJCc3NsRkE")); – Nara Na Oct 24 '16 at 09:24
  • that is a fileID. each file in the Google Drive has a unique fileID – ReyAnthonyRenacia Oct 25 '16 at 18:21
  • So do you know how can i get those fileid from any file by file name? – Nara Na Oct 26 '16 at 05:59
  • something like result.getDriveFile().getDriveId(); as mentioned in [this thread](http://stackoverflow.com/questions/27108178/google-drive-api-android-how-to-obtain-a-drive-file-id). – ReyAnthonyRenacia Oct 26 '16 at 06:12
  • that's the programmatic approach, manual way is added in my answer. – ReyAnthonyRenacia Oct 26 '16 at 06:29
  • The first link is dead, https://developers.google.com/drive/android/metadata, you might now at best change to just the general link to https://developers.google.com/drive/api – questionto42 Mar 08 '22 at 12:26