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.