简体   繁体   English

Android-Google Drive API文件从不同步

[英]Android - Google Drive API file never syncs

I have a weird bug I'm hoping someone can help me with. 我有一个怪异的错误,希望有人可以帮助我。

I am using the Google Drive app folder to backup some data from my app. 我正在使用Google云端硬盘应用文件夹来备份我的应用中的一些数据。 It creates the file and writes my data without and issue, I can sign grab the file from drive at any time and it works fine. 它会创建文件并写入我的数据而不会出现问题,我可以随时从驱动器上签名来抓取文件,并且工作正常。

However if I uninstall the app when I reinstall it the Google Drive App folder is empty. 但是,如果在重新安装应用程序时将其卸载,则Google Drive App文件夹为空。 I'm assuming I'm missing some point of the sync process so it's only caching locally but I can't see what I've done wrong. 我假设我错过了同步过程的某些方面,所以它只是在本地缓存,但看不到我做错了什么。 My code to create and commit the file is below. 我创建和提交文件的代码如下。

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();

According to the documentation , contents.getParcelFileDescriptor() only works with DriveFile.MODE_READ_WRITE . 根据文档contents.getParcelFileDescriptor()仅与DriveFile.MODE_READ_WRITE一起DriveFile.MODE_READ_WRITE You are using DriveFile.MODE_WRITE_ONLY , so you should be calling contents.getOutputStream() . 您正在使用DriveFile.MODE_WRITE_ONLY ,因此您应该调用contents.getOutputStream()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM