I am using the google Drive API within an android application and would like to create a txt file and upload that text file to Google Drive. For example if the string is: "Hello world", it would create a txt file with the given string and upload it to Google Drive. Is this possible? Thank you,
2 Answers
This can be done. Create a normal text file. See this: How To Read/Write String From A File In Android
Then using API upload it. See this: How to upload a file to Google Drive
Disclaimer: You can find almost everything I'm writing in this link.
First, Have you authorized your app to run the GoogleDrive api?
You need to register it in the Google API Manager and submit your SHA1 key. You can get that key by going to your SDK/bin folder and running the keytool utility with this code:
keytool -exportcert -alias androiddebugkey -keystore /path-to-your-debug.keystore -list -v
Now that you have the autorization, it is time to add the google services API to your code. Go to the build.gradle file and add the service:
dependencies {
compile 'com.google.android.gms:play-services:9.2.0'
}
You will have to sync your project again and maybe download some libs.
Finally you can use the API. Create a connection with the Google Drive API:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstance);
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
and then connect:
mGoogleApiClient.connect();
Okay, now you are connected (unless the user refused to connect with his Drive account and you will need to treat that).
Now create your file:
DriveFile myFile
Do what you want to do with the file and then save it:
file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveContentsResult>() {
@Override
public void onResult(DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
// Handle error
return;
}
DriveContents driveContents = result.getDriveContents();
}
});
Hope I could help!

- 206
- 2
- 5