簡體   English   中英

從Android應用上傳Google Drive Sqlite數據庫文件

[英]Google Drive Sqlite db file upload from android app

我可以使用以下信息將數據庫上傳到雲端硬盤。 Drive API-下載/上傳sql數據庫

但如果不使用應用程序,我將無法直接離線訪問它。

目的:在其他應用程序中進一步使用db文件,因此,當我直接從Google驅動器下載內容時,我希望它采用可用格式。

我正在使用MODE_WRITE_ONLY將文件上傳到應用內

mfile.open(api, DriveFile.MODE_WRITE_ONLY, new DriveFile.DownloadProgressListener()      
And mime type as this String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");

當我從手機上的外部sd卡訪問時,我的數據庫大小為44kb,但是當我在驅動器上看到時,它的大小為40kb。 請提出如何使它可讀的方法,以便可以在sqlite瀏覽器中直接打開它,因為當我打開它時,它會顯示“無法識別文件”。

我是否必須對db文件的WRITE only部分或mime類型進行更改。 請提出可能是什么問題。

我不得不承認,我不知道你在問什么。 但是,由於我已經成功測試了將SQLite文件上傳到GooDrive,因此我可以發布一段代碼來完成此操作:

假設您的android設備上有一個SQLite文件:

java.io.File dbFile = Context.getDatabasePath([YOUR_DB_NAME])

然后可以調用此方法:

upload("temp.db", dbFile, "application/x-sqlite3")

com.google.android.gms.common.api.GoogleApiClient GAC;
///...
void upload(final String titl, final File file, final String mime) {
  if (GAC != null && GAC.isConnected() && titl != null  && file != null) try {
    Drive.DriveApi.newDriveContents(GAC).setResultCallback(new ResultCallback<DriveContentsResult>() {
      @Override
      public void onResult(@NonNull DriveContentsResult contRslt) {
        if (contRslt.getStatus().isSuccess()){
          DriveContents cont = contRslt.getDriveContents();
          if (cont != null && file2Os(cont.getOutputStream(), file)) {
            MetadataChangeSet meta = new Builder().setTitle(titl).setMimeType(mime).build();
            Drive.DriveApi.getRootFolder(GAC).createFile(GAC, meta, cont).setResultCallback(
              new ResultCallback<DriveFileResult>() {
                @Override
                public void onResult(@NonNull DriveFileResult fileRslt) {
                  if (fileRslt.getStatus().isSuccess()) {
                    // fileRslt.getDriveFile();   BINGO !!!
                  }
                }
              }
            );
          }
        }
      }
    });
  } catch (Exception e) { e.printStackTrace(); }
}

static boolean file2Os(OutputStream os, File file) {
  boolean bOK = false;
  InputStream is = null;
  if (file != null && os != null) try {
    byte[] buf = new byte[4096];
    is = new FileInputStream(file);
    int c;
    while ((c = is.read(buf, 0, buf.length)) > 0)
      os.write(buf, 0, c);
    bOK = true;
  } catch (Exception e) {e.printStackTrace();}
  finally {
    try {
      os.flush(); os.close();
      if (is != null )is.close();
    } catch (Exception e) {e.printStackTrace();}
  }
  return  bOK;
}

在GooDrive的根目錄中創建一個“ temp.db” SQLite文件。

如果需要將文件放置在其他位置,則當然可以提供其他父文件夾(而不是Drive.DriveApi.getRootFolder(GAC) )。

我希望這是您想要完成的。 祝好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM