我正在尝试将某些文件上传到Google云端硬盘。我在外部存储器中有这个文件。所以,正如我所说,完成它的步骤:
我成功设法创建并找到了文件,但我无法正确编辑它的内容。而且我不知道为什么 - 回调显示,我编辑了文件,在驱动器上它是空的。
这是我上传文件的代码:
public class UploadFileActivity extends BaseActivity {
private final static String TAG = "UploadFileActivity";
@Override
public void onConnected(Bundle connectionHint) {
super.onConnected(connectionHint);
// create new contents resource.
Drive.DriveApi.newDriveContents(getGoogleApiClient()).setResultCallback(driveContentsCallback);
}
final private ResultCallback<DriveApi.DriveContentsResult> driveContentsCallback =
new ResultCallback<DriveApi.DriveContentsResult>() {
@Override
public void onResult(DriveApi.DriveContentsResult result) {
if (!result.getStatus().isSuccess()) {
//if failure
showMessage("Error while trying to create new file contents");
return;
}
MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
.setTitle("editor_backup.xml")
.setMimeType("text/xml")
.setStarred(true).build();
Drive.DriveApi.getRootFolder(getGoogleApiClient())
.createFile(getGoogleApiClient(), changeSet, result.getDriveContents())
.setResultCallback(fileCallBack);
}
};
final ResultCallback<DriveFolder.DriveFileResult> fileCallBack = new ResultCallback<DriveFolder.DriveFileResult>() {
@Override
public void onResult(DriveFolder.DriveFileResult driveFileResult) {
if (!driveFileResult.getStatus().isSuccess()) {
Log.v(TAG, "Error while trying to create the file");
return;
}
//Initialize mFile to be processed in AsyncTask
DriveFile mfile = driveFileResult.getDriveFile();
new EditContentsAsyncTask(UploadFileActivity.this).execute(mfile);
}
};
public class EditContentsAsyncTask extends ApiClientAsyncTask<DriveFile, Void, Boolean> {
public EditContentsAsyncTask(Context context) {
super(context);
}
@Override
protected Boolean doInBackgroundConnected(DriveFile... args) {
DriveFile file = args[0];
try {
DriveApi.DriveContentsResult driveContentsResult = file.open(
getGoogleApiClient(), DriveFile.MODE_WRITE_ONLY, null).await();
if (!driveContentsResult.getStatus().isSuccess()) {
return false;
}
DriveContents driveContents = driveContentsResult.getDriveContents();
//edit the outputStream
String inFileName = Environment.getExternalStorageDirectory().getPath() + "/Android/data/somePath/editor.xml" ;
File backupFile = new File(inFileName);
FileInputStream inputStream = new FileInputStream(backupFile);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
byte[] buffer = new byte[8 * 1024];
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(driveContents.getOutputStream());
int n = 0;
while ((n = bufferedInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer, 0, n);
}
bufferedInputStream.close();
com.google.android.gms.common.api.Status status =
driveContents.commit(getGoogleApiClient(), null).await();
return status.getStatus().isSuccess();
} catch (IOException e) {
Log.e(TAG, "IOException while appending to the output stream", e);
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
if (!result) {
showMessage("Error while editing contents");
return;
}
showMessage("Successfully edited contents");
}
}
}
你能给我一个提示或告诉我我做错了什么吗? 提前谢谢。
答案 0 :(得分:1)
好吧,我找到了解决方案。而且,由于我还没有找到类似的问题,我不会删除这个问题,但会为后代提供答案:)
您必须更改这些行:
while ((n = bufferedInputStream.read(buffer)) > 0) {
bufferedOutputStream.write(buffer, 0, n);
}
致:
while ((n = bufferedInputStream.read(buffer, 0, buffer.length)) > 0) {
bufferedOutputStream.write(buffer, 0, n);
bufferedOutputStream.flush();
}
它是什么。