无法将文字附加到Google云端硬盘文件中

时间:2015-03-11 20:30:13

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

我正在尝试将文字附加到Google云端硬盘上的文字文件中。但是当我写作时,整个文件都会被覆盖。为什么我不能在文件的末尾添加文本?​​

  DriveFile file = Drive.DriveApi.getFile(mGoogleApiClient, id);
  file.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, null).setResultCallback(new ResultCallback<DriveApi.DriveContentsResult>() {
        @Override
          public void onResult(DriveApi.DriveContentsResult driveContentsResult) {
                  msg.Log("ContentsOpenedCallBack");

                  if (!driveContentsResult.getStatus().isSuccess()) {
                     Log.i("Tag", "On Connected Error");
                     return;
                  }

                  final DriveContents driveContents = driveContentsResult.getDriveContents();

                  try {
                     msg.Log("onWrite");
                     OutputStream outputStream = driveContents.getOutputStream();
                     Writer writer = new OutputStreamWriter(outputStream);
                     writer.append(et.getText().toString());
                     writer.close();
                     driveContents.commit(mGoogleApiClient, null);

                  } catch (IOException e) {
                     e.printStackTrace();
                  }
             }

 });

3 个答案:

答案 0 :(得分:2)

最后,我找到了将文字附加到驱动器文档上的答案。

 DriveContents contents = driveContentsResult.getDriveContents();

 try {

      String input = et.getText().toString();

      ParcelFileDescriptor parcelFileDescriptor = contents.getParcelFileDescriptor();
      FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor
                            .getFileDescriptor());

      // Read to the end of the file.

     fileInputStream.read(new byte[fileInputStream.available()]);


      // Append to the file.
      FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
                            .getFileDescriptor());
      Writer writer = new OutputStreamWriter(fileOutputStream);
      writer.write("\n"+input);

      writer.close();
      driveContentsResult.getDriveContents().commit(mGoogleApiClient, null);

 } catch (IOException e) {
                    e.printStackTrace();
     }

SO

答案 1 :(得分:0)

原因是提交的默认解决策略是覆盖现有文件。检查API docs,看看是否有办法追加更改。

答案 2 :(得分:0)

对于2017年遇到此问题的任何人: 谷歌有一些附加数据的方法以下是a link

虽然从谷歌复制方法对我来说并不完全有效,但是这里是附加数据的类:(请注意这是此代码link的修改版本)

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.gms.common.api.Result;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.drive.Drive;
import com.google.android.gms.drive.DriveApi.DriveContentsResult;
import com.google.android.gms.drive.DriveApi.DriveIdResult;
import com.google.android.gms.drive.DriveContents;
import com.google.android.gms.drive.DriveFile;
import com.google.android.gms.drive.DriveId;

/**
* An activity to illustrate how to edit contents of a Drive file.
*/
public class EditContentsActivity extends BaseDemoActivity {

private static final String TAG = "EditContentsActivity";

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);

    final ResultCallback<DriveIdResult> idCallback = new ResultCallback<DriveIdResult>() {
        @Override
        public void onResult(DriveIdResult result) {
            if (!result.getStatus().isSuccess()) {
                showMessage("Cannot find DriveId. Are you authorized to view this file?");
                return;
            }
            DriveId driveId = result.getDriveId();
            DriveFile file = driveId.asDriveFile();
            new EditContentsAsyncTask(EditContentsActivity.this).execute(file);
        }
    };
    SharedPreferences sp= PreferenceManager.getDefaultSharedPreferences(EditContentsActivity.this);
    Drive.DriveApi.fetchDriveId(getGoogleApiClient(), EXISTING_FILE_ID)
            .setResultCallback(idCallback);
}

public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {

    public EditContentsAsyncTask(Context context) {
        super(context);
    }

    @Override
    protected Boolean doInBackgroundConnected(DriveFile... args) {
        DriveFile file = args[0];
        SharedPreferences sp=PreferenceManager.getDefaultSharedPreferences(EditContentsActivity.this);
        System.out.println("0"+sp.getString("drive_id","1"));
        DriveContentsResult driveContentsResult=file.open(getGoogleApiClient(), DriveFile.MODE_READ_WRITE, null).await();
                System.out.println("1");
        if (!driveContentsResult.getStatus().isSuccess()) {
            return false;
        }
        DriveContents driveContents = driveContentsResult.getDriveContents();
                try {
                    System.out.println("2");
                    ParcelFileDescriptor parcelFileDescriptor = driveContents.getParcelFileDescriptor();
                    FileInputStream fileInputStream = new FileInputStream(parcelFileDescriptor
                            .getFileDescriptor());
                    // Read to the end of the file.
                    fileInputStream.read(new byte[fileInputStream.available()]);
                    System.out.println("3");
                    // Append to the file.
                    FileOutputStream fileOutputStream = new FileOutputStream(parcelFileDescriptor
                            .getFileDescriptor());
                    Writer writer = new OutputStreamWriter(fileOutputStream);
                    writer.write("hello world");
                    writer.close();
                    System.out.println("4");
                    driveContents.commit(getGoogleApiClient(), null).await();
                    return true;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return false;
        };








    @Override
    protected void onPostExecute(Boolean result) {
        if (!result) {
            showMessage("Error while editing contents");
            return;
        }
        showMessage("Successfully edited contents");
    }
}
}

Existing_File_id是资源ID。如果您需要资源ID a link

,这是一个链接