1

Possible Duplicate:
Android Open and Save files to/from Google Drive SDK

I'm attempting to upload and download files from Google Drive in my android application.

I was able to obtain an authorization token string for the user's Google account using AccountManager.getAuthToken().

Now, I don't know how to use that token to initialize the Drive object to start sending requests to Google Drive. I used the following code from an example in Google Drive scarce documentation, but it doesn't work. Apparently, the call succeeds, but when I try to upload a file, it just hangs up until Android tells me that the App is not responding.

GoogleCredential credential = new GoogleCredential().setAccessToken(authToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Drive drive = null;

drive = Drive.builder(httpTransport, jsonFactory)
    .setApplicationName(APP_NAME)
    .setHttpRequestInitializer(credential)
    .setJsonHttpRequestInitializer(new GoogleKeyInitializer(API_KEY))
    .build();

To upload a certain file I do the following:

try
{
    // local file
    java.io.File fileLocal = new java.io.File(FILE_NAME);

    // remote file

    File remoteFile = new File();
    remoteFile.setTitle(fileLocal.getName());
    remoteFile.setDescription("Text File");
    remoteFile.setMimeType("text/plain");

    // load the content of the local file into the remote content
    FileContent remoteFileContent = new FileContent("text/plain", fileLocal);

    // upload the file into Google Drive
    File resultFile = drive.files().insert(remoteFile, remoteFileContent).execute();

    lblStatus.setText("Upload successful with file ID: " + resultFile.getId());
}
catch (Exception e)
{
    // error handling
    ...
}

I've read the Google API documents and they are a mess, and the classes don't have a proper documentation. The samples they give are quite messy.

Any idea why is the app not responding? What am I doing wrong? Any help is appreciated. Thanks.

Community
  • 1
  • 1
sidezr
  • 163
  • 1
  • 12

1 Answers1

0

I was having this issue a few weeks ago. Have a look over here, where I wrote a pretty extensive explanation of how to make Google Drive mostly work on Android. (I say mostly because file downloads don't seem to be properly authorized...)

Android Open and Save files to/from Google Drive SDK

Community
  • 1
  • 1
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193