How to write into Drive application data folder?

2019-05-11 15:03发布

问题:

I'm using this tutorial for writing on Drive from Android. I have a file on the internal storage, and I want to copy it on Drive, like this:

FileOutputStream fos = openFileOutput(filename,
        Context.MODE_PRIVATE);
fos.write("hello");
fos.close();

The section "Inserting a file into the Application Data folder" of the tutorial says:

// File's content.
java.io.File fileContent = new java.io.File(filename);
FileContent mediaContent = new FileContent(mimeType, fileContent);
try {
  File file = service.files().insert(body, mediaContent).execute();
  return file;
//...

How should I do?

EDIT: I would expect this code to work, but it does not:

// File's content.
java.io.File fileContent = new java.io.File(filename);
FileOutputStream fos = new FileOutputStream(fileContent);
fos.write("hello");
fos.close();
FileContent mediaContent = new FileContent(mimeType, fileContent);
try {
  File file = service.files().insert(body, mediaContent).execute();
  return file;
//...

EDIT:

I get these error, both of which I don't know how to solve:

04-15 12:12:24.615: W/AbstractGoogleClient(2272): Application name is not set. Call Builder#setApplicationName.
04-15 12:12:24.615: I/System.out(2272): An error occured: java.io.FileNotFoundException: /filename: open failed: EROFS (Read-only file system)