Upload text file to Google Drive using Android

这一生的挚爱 提交于 2019-12-05 11:51:58
seanpj

Assuming that you question is: 'How do I upload a text file to Google Drive?', here is the quick overview:

1/ Get your app authorized on developers console, see this. Basically, tell Google that your app represented by SHA1 / 'package-name' needs access to Drive API (don't forget your email address on the consent screen). This authorization is good for both REST and GDAA api.

2/ Decide if you want to use REST or GDAA API to access the Drive. Each has advantages/disadvantages (but it's a long story).

3/ Take a look at the REST/GDAA wrapper demo here, it has the app authorization process in the MainActivity class (see onConnFail() method), and basic CRUD methods for both REST and GDAA in their respective classes.

Good Luck

UPDATE
Based on your comments below, I assume you want to force the QuickStart demo to work for you. Keep in mind that GDAA (or REST) don't care what the content is It is just a bunch of bytes. So, as QuickStart turns the Bitmap into PNG and feeds the output stream with it's bytes, you have to do it with your bunch-of-bytes. I quickly smacked together 2 primitives below, that would feed the DriveContents' output stream with file or byte array (and you can turn whatever you've got into a file or byte[]).

 DriveContents file2Cont(DriveContents driveContents, java.io.File file) {
    OutputStream oos = driveContents.getOutputStream();
    if (oos != null) try {
      InputStream is = new FileInputStream(file);
      byte[] buf = new byte[8192];
      int c = 0;
      while ((c = is.read(buf, 0, buf.length)) > 0) {
        oos.write(buf, 0, c);
        oos.flush();
      }
    } catch (Exception e)  {/*handle errors*/}
    finally {
      try {
        oos.close();
      } catch (Exception ignore) { }
    }
    return driveContents;
  }

  DriveContents bytes2Cont(DriveContents driveContents, byte[] buf) {
    OutputStream os = driveContents.getOutputStream();
    try { os.write(buf);
    } catch (IOException e)  {/*handle errors*/}
     finally {
      try { os.close();
      } catch (Exception e) {/*handle errors*/}
    }
    return driveContents;
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!