How to upload a db file to google drive from android application?

为君一笑 提交于 2019-12-01 06:43:26
seanpj

First, the fact that I'm seeing these imports:

import com.google.api.client.http.FileContent;
...
import com.google.api.services.drive.Drive;

indicates that you're mixing GDAA and the REST Api. Do not do it unless you want to get in trouble.

So, when you got your FolderId, it is a parent of your 'DB' file, I presume (and if the DB file is a SQLite type, your MIME TYPE should be 'application/x-sqlite3').

Anyway, in GDAA, you first have to turn your java.io.File (that represents the 'DB') into content. And you have to supply metadata (like file title, mime type, flags ...). You got the metadata right, it is the content you tripped over. The following snippet should do what you need (don't hold me accountable, I slammed it together in 5 minutes - no testing)

/******************************************************************
   * create file in GOODrive
   * @param pFldr parent's ID
   * @param titl  file name
   * @param mime  file mime type  (application/x-sqlite3)
   * @param file  file (with content) to create
   */
  static void saveToDrive(final DriveFolder pFldr, final String titl, 
                                      final String mime, final java.io.File file) {
    if (getGoogleApiClient() != null && pFldr != null && titl != null && mime != null && file != null) try {
      // create content from file
      Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(new ResultCallback<DriveContentsResult>() {
        @Override
        public void onResult(DriveContentsResult driveContentsResult) {
          DriveContents cont = driveContentsResult != null && driveContentsResult.getStatus().isSuccess() ?
            driveContentsResult.getDriveContents() : null;

          // write file to content, chunk by chunk   
          if (cont != null) try {
            OutputStream oos = cont.getOutputStream();
            if (oos != null) try {
              InputStream is = new FileInputStream(file);
              byte[] buf = new byte[4096];
              int c;
              while ((c = is.read(buf, 0, buf.length)) > 0) {
                oos.write(buf, 0, c);
                oos.flush();
              }
            }
            finally { oos.close();}

            // content's COOL, create metadata
            MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build();

            // now create file on GooDrive
            pFldr.createFile(getGoogleApiClient(), meta, cont).setResultCallback(new ResultCallback<DriveFileResult>() {
              @Override
              public void onResult(DriveFileResult driveFileResult) {
                if (driveFileResult != null && driveFileResult.getStatus().isSuccess()) {
                  // BINGO
                } else {
                  // report error
                }
              }
            });
          } catch (Exception e) { e.printStackTrace(); }
        }
      });
    } catch (Exception e) { e.printStackTrace(); }
  }

I noticed, you're adapting the 'official' GDAA DEMO for your project. In case it is not sufficient or overwhelming, you may also look at this demo, that takes a different approach to the same problem.

Good Luck

Chandra Prabhu Thuvariman

Check your mime type of database file.

private String getMimeType(String string) {
    //set return "application/x-sqlite3";
    return "application/x-sqlite3";
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!