简体   繁体   中英

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!

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM