有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android Google Drive API文件从不同步

我有一个奇怪的错误,我希望有人能帮我解决

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

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

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.安卓.gms.common.api.Status>() {
                    @Override
                    public void onResult(com.google.安卓.gms.common.api.Status result) {
                        if(result.getStatus().isSuccess())
                            Log.i(TAG, "Write succeeded");
                        else
                            Log.i(TAG, "Write failed");
                        onConnectionCallbacks.onSynced(x);
                    }
                });
}
        }.execute();

共 (1) 个答案

  1. # 1 楼答案

    根据documentationcontents.getParcelFileDescriptor()只适用于DriveFile.MODE_READ_WRITE。您正在使用DriveFile.MODE_WRITE_ONLY,因此应该调用contents.getOutputStream()