【问题标题】:Android - Google Drive API file never syncsAndroid - Google Drive API 文件从不同步
【发布时间】:2014-12-20 21:31:50
【问题描述】:

我有一个奇怪的错误,希望有人能帮助我。

我正在使用 Google Drive 应用程序文件夹从我的应用程序中备份一些数据。它创建文件并在没有问题的情况下写入我的数据,我可以随时从驱动器中签名抓取文件,它工作正常。

但是,如果我在重新安装该应用程序时将其卸载,则 Google Drive App 文件夹为空。我假设我错过了同步过程的某些点,所以它只是在本地缓存,但我看不出我做错了什么。我创建和提交文件的代码如下。

DriveFile file = ..//Get drive file;
file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null)
                .setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
                    @Override
                    public void onResult(DriveApi.DriveContentsResult result) {
                        if (!result.getStatus().isSuccess()) {
                            Log.i(TAG, "Problem opening file, status is " + result.getStatus().toString());
                            return;
                        }
                        Log.i(TAG, "File opened");
                        writeFileContents(result.getDriveContents());
                    }
                });

private void writeFileContents(final DriveContents contents) {
new AsyncTask<Object, Object, Integer>() {

            @Override
            protected Integer doInBackground(Object[] params) {
try {
                    ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
                    // Overwrite the file.
                    FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
                            .getFileDescriptor());
                    Writer writer = new OutputStreamWriter(fileOutputStream);
                    SyncUtils.writeXml("some xml", writer);
                    writer.close();
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return;
            }

            @Override
        protected void onPostExecute(final Integer x) {
                contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback<com.google.android.gms.common.api.Status>() {
                    @Override
                    public void onResult(com.google.android.gms.common.api.Status result) {
                        if(result.getStatus().isSuccess())
                            Log.i(TAG, "Write succeeded");
                        else
                            Log.i(TAG, "Write failed");
                        onConnectionCallbacks.onSynced(x);
                    }
                });
}
        }.execute();

【问题讨论】:

  • 您是如何配置身份验证的?我遇到了类似的问题,并被告知我使用了错误的身份验证类型...
  • 嗯..这是你的意思吗? mGoogleApiClient = new GoogleApiClient.Builder(context) .addApi(Drive.API) .addScope(Drive.SCOPE_APPFOLDER) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();
  • 啊,这是另一种初始化我不知道的客户端的方法。嗯。 Google 的 API 太复杂了……
  • 在以前版本的 Play 服务中存在一个问题,即重新安装应用后应用数据无法正确同步。如果您没有使用 Play Services 6.5 或更高版本,请尝试一下。否则,您能否在问题跟踪器上针对 Drive Android API 创建一个错误? code.google.com/a/google.com/p/apps-api-issues

标签: java android google-drive-api google-drive-android-api


【解决方案1】:

根据documentationcontents.getParcelFileDescriptor() 仅适用于DriveFile.MODE_READ_WRITE。你正在使用DriveFile.MODE_WRITE_ONLY,所以你应该打电话给contents.getOutputStream()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多