简体   繁体   English

从Android应用上传Google Drive Sqlite数据库文件

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

I'm able to upload database to Drive using the following post. 我可以使用以下信息将数据库上传到云端硬盘。 Drive API - Download/upload sql database Drive API-下载/上传sql数据库

But I'm not able to access it directly offline without using app. 但如果不使用应用程序,我将无法直接离线访问它。

Aim: Use the db file further in different application so I want it to be in a usable format whenever I download the content directly from google drive. 目的:在其他应用程序中进一步使用db文件,因此,当我直接从Google驱动器下载内容时,我希望它采用可用格式。

I am using MODE_WRITE_ONLY to upload the file to drive from within app 我正在使用MODE_WRITE_ONLY将文件上传到应用内

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

My db size is 44kb when I access from external sd card on phone, however it shows 40kb when I see on drive. 当我从手机上的外部sd卡访问时,我的数据库大小为44kb,但是当我在驱动器上看到时,它的大小为40kb。 Please suggest what can I do to make it readable so that I can directly open it in an sqlite browser because when I open it shows "File not recognized". 请提出如何使它可读的方法,以便可以在sqlite浏览器中直接打开它,因为当我打开它时,它会显示“无法识别文件”。

Do I have to make changes in the WRITE only part or mime type for db file. 我是否必须对db文件的WRITE only部分或mime类型进行更改。 Please suggest what could be the problem. 请提出可能是什么问题。

I have to admit, I have no idea what you're asking. 我不得不承认,我不知道你在问什么。 But since I've successfully tested an SQLite file upload to GooDrive, I can post a piece of code that does it: 但是,由于我已经成功测试了将SQLite文件上传到GooDrive,因此我可以发布一段代码来完成此操作:

Let's assume, there is a SQLite file on your android device: 假设您的android设备上有一个SQLite文件:

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

Then you can call this method: 然后可以调用此方法:

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;
}

To create a "temp.db" SQLite file in the root of your GooDrive. 在GooDrive的根目录中创建一个“ temp.db” SQLite文件。

You can certainly supply a different parent folder (instead of Drive.DriveApi.getRootFolder(GAC) ) if you need to place your file in a different location. 如果需要将文件放置在其他位置,则当然可以提供其他父文件夹(而不是Drive.DriveApi.getRootFolder(GAC) )。

I hope that this is what you wanted to accomplish. 我希望这是您想要完成的。 Good Luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文件从Android应用上传到Google驱动器 - Upload file from android app to google drive 如何从Android应用程序上传db文件到谷歌驱动器? - How to upload a db file to google drive from android application? Google Drive Sqlite数据库上传不起作用 - Google Drive Sqlite db upload not working 无法使用教程将文件上传到Google云端硬盘(Android上为Drive App) - Unable To Upload File to Google Drive Using Tutorial (Drive App on Android) Google Drive Android API-从驱动器下载数据库文件 - Google Drive Android api - Downloading db file from drive 将Google云端硬盘实施到Android应用并从Google云端硬盘下载文件 - Google Drive Implementation to Android app and Downloading the file from Google drive 如何在Android App中创建上传文件到Google云端硬盘 - how to create upload file to Google Drive in Android App 使用 Google 登录从 Android 设备将文件上传到 Google Drive - Upload a file to Google Drive using Google Sign In from Android device 从用户Google云端硬盘中选择任何文件,然后通过Android应用将其发送(上传)到我的服务器 - Pick any file from users Google Drive and send (Upload) it to my server via Android app 如何从Android App将文件上传到Google Drive(GoogleDrive API集成) - How to upload a file in to Google Drive from Android App(GoogleDrive API integration)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM