将SQLite数据库同步到谷歌驱动器,无需确认

时间:2014-10-01 14:13:48

标签: android upload google-drive-api

我必须在我的Android应用程序中实现同步。所以,我需要使用用户的谷歌驱动器来存储和检索信息。我已经创建了将* .db文件存储到驱动器的功能。但问题是用户每次都必须确认文件上传操作。我想在没有确认的情况下在每个应用关闭上传文件。可能吗 ?是否有其他方法可以在没有自己的服务器的情况下同步sqlite db?

public static void backUpDb(final Activity context){
    mGoogleApiClient = new GoogleApiClient.Builder(context)
    .addApi(Drive.API)
    .addScope(Drive.SCOPE_FILE)
    .addConnectionCallbacks(new ConnectionCallbacks() {
        @Override
        public void onConnectionSuspended(int arg0) {
        }

        @Override
        public void onConnected(Bundle arg0) {

            Log.i(TAG, "API client connected.");
            saveFileToDrive( context);
        }
    })
    .addOnConnectionFailedListener(
            new OnConnectionFailedListener() {

                @Override
                public void onConnectionFailed(
                        ConnectionResult result) {
                    Log.i(TAG,
                            "GoogleApiClient connection failed: "
                                    + result.toString());
                    if (!result.hasResolution()) {
                        // show the localized error
                        // dialog.
                        GooglePlayServicesUtil
                                .getErrorDialog(
                                        result.getErrorCode(),
                                        context, 0).show();
                        return;
                    }

                    try {
                        result.startResolutionForResult(
                                context,
                                REQUEST_CODE_RESOLUTION);
                    } catch (SendIntentException e) {
                        Log.e(TAG,
                                "Exception while starting resolution activity",
                                e);
                    }

                }
            }).build();
    mGoogleApiClient.connect();


    //mGoogleApiClient.disconnect();        
}



public static void saveFileToDrive(final Activity context) {
    Drive.DriveApi.newContents(mGoogleApiClient).setResultCallback(
            new ResultCallback<ContentsResult>() {

                @Override
                public void onResult(ContentsResult result) {
                    if (!result.getStatus().isSuccess()) {

                        return;
                    }
                    OutputStream outputStream = result.getContents()
                            .getOutputStream();
                    try {
                        // outputStream.write(bitmapStream.toByteArray());

                        //ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
                        File dbFile = context.getDatabasePath(databaseName);
                        //dbFile.
                        int size = (int) dbFile.length();
                        byte[] bytes = new byte[size];
                        BufferedInputStream buf = new BufferedInputStream(new FileInputStream(dbFile));
                        buf.read(bytes, 0, bytes.length);
                        buf.close();

                        byte[] b = "aa".getBytes();
                        outputStream.write(bytes);
                    } catch (IOException e1) {
                        Log.i(TAG, "Unable to write file contents.");
                    }

                    String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
                    MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                            .setMimeType(mimeType).setTitle("aldkan.db")
                            .build();
                    IntentSender intentSender = Drive.DriveApi
                            .newCreateFileActivityBuilder()
                            .setInitialMetadata(metadataChangeSet)
                            .setInitialContents(result.getContents())
                            .build(mGoogleApiClient);
                    try {
                        context.startIntentSenderForResult(intentSender, 2,
                                null, 0, 0, 0);
                    } catch (SendIntentException e) {

                        e.printStackTrace();
                    }
                }
            });
}

1 个答案:

答案 0 :(得分:0)

CreateFileActivity是一种为Google云端硬盘构建文件上传界面的便捷方式。但是,您可以直接使用API​​创建和修改文件,如Creating Files指南中所述,以避免任何用户交互。