Setting up the environment for the first time
First, I needed the google play services library, which I setup using this guide. I used the "Android SDK Manager" from Eclipse to download the SDK. This took a while to complete, because installation of one item required an upgrade or installation of a few other dependent libraries and tools. In all, it took me about an hour.
I then followed this guide to update my Android project to work with the Google Play Services and Drive API. At this point, I should have been able to:
- Connect your app to Google Drive.
- Ask you which account you'd like to use.
- Allow you to save a picture that you take to Google Drive.
- My app was crashing with a message about "No Activity found to handle Intent { act=com.google.android.gms.common.account.CHOOSE_ACCOUNT (has extras) }". As suggested by a stack overflow question, I tried running my app on a real phone (instead of with an emulator) and it worked without crashing.
- When I tried to save a file to Google drive I got a 403 error with a meesage about "Access Not Configured". I realized I set up a client id using the production version of my keystore. I had to use the development version from eclipse with:
keytool -exportcert -alias androiddebugkey -keystore .android/debug.keystore -list -v
where the password was 'android'.
My business logic
For my app, I'd like to.
- Create a new csv (spreadsheet) file on Google Drive.
- Check if a previously saved file from my app exists on Google Drive, and read it.
- If a file exists, update the existing csv file with updated data.
1. Creating a new Google Drive Spreadsheet file.
After creating a comma separated file, I saved it to Google Drive with the function listed below. Importantly, you have to call setConvert(true) in order to allow Google Drive to update the file.
After creating a comma separated file, I saved it to Google Drive with the function listed below. Importantly, you have to call setConvert(true) in order to allow Google Drive to update the file.
private String m_filename = "someFile";
private void saveFileToDrive() {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
// File's metadata.
File body = new File();
body.setTitle(m_filename);
body.setDescription("Description for "+m_filename);
body.setMimeType("text/csv");
// File's content
String filePath = getApplicationContext().getFilesDir() +
java.io.File.separator +
m_filename;
java.io.File fileContent = new java.io.File(filePath);
FileContent mediaContent = new FileContent("test/csv",
fileContent);
// Insert and convert to spreadsheet
Insert request = m_service.files().insert(body, mediaContent);
request.setConvert( true );
File file = request.execute();
if (file != null) {
Log.i("MyTag","saveFileToDrive file Id:"+file.getId());
}
} catch (UserRecoverableAuthIOException e) {
startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
Log.e("MyTag","Error code: " + error.getCode());
Log.e("MyTag","Error message: " + error.getMessage());
} catch (IOException e) {
Log.e("MyTag","saveFileToDrive failed");
e.printStackTrace();
}
}});
t.start();
}
No comments:
Post a Comment