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

前端 未结 2 1409
鱼传尺愫
鱼传尺愫 2021-01-13 18:19

I want to upload a db file from my app to google drive. I am able to create a folder in google drive but I am not getting how to upload db file .

This is my code.

相关标签:
2条回答
  • 2021-01-13 18:53

    Check your mime type of database file.

    private String getMimeType(String string) {
        //set return "application/x-sqlite3";
        return "application/x-sqlite3";
    }
    
    0 讨论(0)
  • 2021-01-13 19:03

    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

    0 讨论(0)
提交回复
热议问题