【问题标题】:Upload sql database to google drive using drive api使用驱动器api将sql数据库上传到谷歌驱动器
【发布时间】:2016-03-10 21:00:24
【问题描述】:

我想在我的 Android 应用中将数据库文件上传到 Google 驱动器。

我可以很好地备份保存到 /sdcard/schoolbinder/backups/notes 的数据库文件

我从这个网站开始:https://github.com/googledrive/android-quickstart/blob/master/app/src/main/java/com/google/android/gms/drive/sample/quickstart/MainActivity.java

这在我的应用程序中可以正常工作,但它是图片而不是文件。关于如何上传文件的任何想法

【问题讨论】:

  • 看看api可能会很好
  • 我上传的部分对我来说毫无意义,我的问题中的这段代码在我的应用程序中工作它只是做图片而不是文件,如果有人知道如何将它缩小到做文件将全部设置

标签: java android google-drive-api


【解决方案1】:

您引用的演示已经有选择文件的示例。

/**
 * Create a new file and save it to Drive.
 */
private void saveFileToDrive() {
    // Start by creating a new contents, and setting a callback.
    Log.i(TAG, "Creating new contents.");
    final Bitmap image = mBitmapToSave;
    Drive.DriveApi.newDriveContents(mGoogleApiClient)
            .setResultCallback(new ResultCallback<DriveContentsResult>() {

        @Override
        public void onResult(DriveContentsResult result) {
            // If the operation was not successful, we cannot do anything
            // and must
            // fail.
            if (!result.getStatus().isSuccess()) {
                Log.i(TAG, "Failed to create new contents.");
                return;
            }
            // Otherwise, we can write our data to the new contents.
            Log.i(TAG, "New contents created.");
            // Get an output stream for the contents.
            OutputStream outputStream = result.getDriveContents().getOutputStream();
            // Write the bitmap data from it.
            ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
            try {
                outputStream.write(bitmapStream.toByteArray());
            } catch (IOException e1) {
                Log.i(TAG, "Unable to write file contents.");
            }
            // Create the initial metadata - MIME type and title.
            // Note that the user will be able to change the title later.
            MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                    .setMimeType("image/jpeg").setTitle("Android Photo.png").build();
            // Create an intent for the file chooser, and start it.
            IntentSender intentSender = Drive.DriveApi
                    .newCreateFileActivityBuilder()
                    .setInitialMetadata(metadataChangeSet)
                    .setInitialDriveContents(result.getDriveContents())
                    .build(mGoogleApiClient);
            try {
                startIntentSenderForResult(
                        intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
            } catch (SendIntentException e) {
                Log.i(TAG, "Failed to launch file chooser.");
            }
        }
    });
}

这只是调用它的问题。该演示似乎正在调用相机应用程序并返回Image。尝试删除onConnected 方法中的if 条件,应调用saveFileToDrive 并显示文件选择器。

【讨论】:

  • 它仍在寻找图像
  • 您在上面发布的代码中就是我所拥有的。我需要将其转换为文件
  • 你可以只设置 mimeType 你想选择什么文件, MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() .setMimeType("image/jpeg").setTitle("Android Photo.png").build (); MimeType 可以参考sitepoint.com/web-foundations/mime-types-complete-list
猜你喜欢
  • 2018-11-10
  • 2013-04-20
  • 1970-01-01
  • 2015-10-30
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多