Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving a file in Google Drive from Android

Good morning, I am trying to write a simple Android app that just stores a file in Google Drive so that I can retrieve it from other locations. I followed the examples on the Google tutorial, and I'm using exactly their code, but the result produces no error and my file doesn't get stored.

Since I'm using exactly the example code, can anyone suggest what my next step might be to try to get this working?

Edit with more information: I'm using the google quickstart example here: https://developers.google.com/drive/api/v3/quickstart/java

and tried to combine this drivesample in order to upload a file: https://github.com/google/google-api-java-client-samples/blob/master/drive-cmdline-sample/src/main/java/com/google/api/services/samples/drive/cmdline/DriveSample.java

I was expecting to see the result when I go to my google drive page on my computer.

Edit Again: Perhaps my attempt to work the two of these together is what I'm doing wrong. I will also accept as an answer any link to a good step by step tutorial to simply uploading some bytes to Google Drive that will show up as a file!

like image 333
KiraHoneybee Avatar asked Dec 20 '25 07:12

KiraHoneybee


1 Answers

If you are looking for a reference on how to upload files using Drive API in Java, you can refer in this official document Upload file data.

  • Simple upload - upload the media only, without any metadata.
  • Multipart upload - upload both the media and its metadata, in a single request

Sample Java Code (Multipart Upload):

File fileMetadata = new File();
fileMetadata.setName("photo.jpg");
java.io.File filePath = new java.io.File("files/photo.jpg");
FileContent mediaContent = new FileContent("image/jpeg", filePath);
File file = driveService.files().create(fileMetadata, mediaContent)
    .setFields("id")
    .execute();

Note: Your FileContent() expects a file type in its first parameter, you can choose for a specific file type based on the MIME Types provided under the additional references

Additional References:

  • Class FileContent
  • Google Workspace and Drive MIME Types
  • Google Workspace documents and corresponding export MIME types
  • Google Drive API in Android Studio Tutorial (PART 1)
  • Google Drive API in Android Studio Tutorial (PART 2)
  • Upload PDF files to Google Drive using Google Drive SDK in Android Studio PART 3
like image 92
Kristkun Avatar answered Dec 22 '25 21:12

Kristkun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!