How to get the file ID so I can perform a download of a file from Google Drive API on Android?

Boardy picture Boardy · Feb 25, 2013 · Viewed 95.4k times · Source

I am working on an Android project and I am trying to make use of the Google Drive API and I've got most of it working but I am having an issue in how I perform a download.

I can't find anywhere how I get the file ID so that I can perform the download. In order to test I've retrieved all files in my drive account and added them to a list array and then got the ID for the first file in the list.

I then copied the file ID in to the command to download the file and this worked successfully but I have no idea how to get the file id of the specific file I want to download.

Below is the code I am using to download the file.

private void downloadFile()
{
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try 
            {
                com.google.api.services.drive.model.File file = service.files().get("0B-Iak7O9SfIpYk9zTjZvY2xreVU").execute();
                //FileList file = service.files().list().execute();
                //List<com.google.api.services.drive.model.File> fileList = file.getItems();
                //com.google.api.services.drive.model.File fileItem = fileList.get(0);
                //Log.d("FileID" , fileItem.getId());
                //Log.d("Count", "Retreived file list");
                if (file.getDownloadUrl() != null && file.getDownloadUrl().length() > 0)
                {
                    HttpResponse resp = service.getRequestFactory().buildGetRequest(new GenericUrl(file.getDownloadUrl())).execute();
                    InputStream inputStream = resp.getContent();
                    writeToFile(inputStream);
                }
            } 
            catch (IOException e)
            {
                Log.e("WriteToFile", e.toString());
                e.printStackTrace();
            }
        }
    });
    thread.start();
}

0B-Iak7O9SfIpYk9zTjZvY2xreVU is the file ID of the file that I download which I retrieved when I did a list and selected the first file, but how can I say I want to download, File_1 and get its ID to then pass this to the service.get().execute function.

Basically my end goal is within my app, I upload an XML file to Google Drive, and then later on Download the file. It will only be one file and will always have the same name. Am I going about it the right away or is there a better way to achieve what I am trying to do?

Answer

RomRoc picture RomRoc · Feb 18, 2018

In my opinion the easiest and fastest way to get a Google Drive file ID is from Google Drive on the web. Right-click the file name and select Get shareable link. The last part of the link is the file ID. Then you can cancel the sharing.