Reputation: 1586
Can someone show me short example how to send file from Android Phone to Google Drive?
Or Create new File with content and send to Drive.
Google example code on www site have errors or are from the new API but example app using old api..
Google sample:
File fileMetadata = new File();
fileMetadata.setTitle("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().insert(fileMetadata, mediaContent)
.setFields("id")
.execute();
But methods like setTitle() and insert() not exists?
My code:
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
mService = new com.google.api.services.drive.Drive.Builder(
transport, jsonFactory, credential)
.setApplicationName("Drive API Android Quickstart")
.build();
mService.files().create(new File().setName("TITLE").setMimeType("text/csv")).execute();
This creating file with name TITLE and correct MimeType.
But how to send normal java.io.File or how to create content for drive.model.File ??
Upvotes: 1
Views: 687
Reputation: 239
You need to add
implementation 'com.google.apis:google-api-services-drive:v3-rev20191108-1.30.3'
in your Gradle. And then in you class
import com.google.api.services.drive.model.File;
Upvotes: 0
Reputation: 89
Your snippet is close but there are a few differences between it and Google's example:
File fileMetadata = new File();
fileMetadata.setName("config.json");
fileMetadata.setParents(Collections.singletonList("appDataFolder"));
java.io.File filePath = new java.io.File("files/config.json");
FileContent mediaContent = new FileContent("application/json", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
.setFields("id")
.execute();
setName
instead of setTitle
appDataFolder
driveService.files().create
instead of driveService.files().insert
Upvotes: 1
Reputation: 11214
Install the Google Drive app on your device. Well i think you already have.
After that use Intent.ACTION_OPEN_DOCUMENT to let the user select a file from the drive which you then can read and write.
Or use Intent.ACTION_CREATE_DOCUMENT to let the user create a file-name on the drive where you than can copy your file to.
Upvotes: 0