【发布时间】:2014-02-03 17:12:02
【问题描述】:
我一直在按照 Google 的文档在 Google Drive 上创建一个文件夹,然后在其中创建一个 jpeg 文件。 How to create file
然后,我需要生成指向该文件的直接链接。我正在使用以下链接执行此操作:https://docs.google.com/uc?export=download&id=[File ResourceID]
文件在光盘上创建。我可以使用浏览器访问它。 问题是,在 onCreateFile(DriveFolder.DriveFileResult driveFileResult) 回调或其他任何地方,我无法获得正确的 ResourceID。
private void saveFileToDrive(final Bitmap image) {
// Start by creating a new contents, and setting a callback.
Log.i(TAG, "Creating new contents.");
DriveApi.newContents(mGoogleApiClient).addResultCallback(new DriveApi.OnNewContentsCallback() {
@Override
public void onNewContents(final DriveApi.ContentsResult result) {
// If the operation was not successful, we cannot do anything
// and must fail.
if (!result.getStatus().isSuccess()) {
Log.i(TAG, "Failed to create new contents.");
return;
}
// Otherwise, we can write our data to the new contents.
Log.i(TAG, "New contents created. ID:"+result.getContents().getDriveId());
// Get an output stream for the contents.
OutputStream outputStream = result.getContents().getOutputStream();
// Write the bitmap data from it.
ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, bitmapStream);
try {
outputStream.write(bitmapStream.toByteArray());
} catch (IOException e1) {
Log.i(TAG, "Unable to write file contents.");
}
// Create the initial metadata - MIME type and title.
// Note that the user will be able to change the title later.
MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
.setMimeType("image/jpeg")
.setTitle(ds.getFujifilmOrderID()+".jpeg")
.build();
Drive.DriveApi.getFolder(mGoogleApiClient, PhotoPrintOrder.this.MyRealFontFolder).createFile(mGoogleApiClient, metadataChangeSet, result.getContents()).addResultCallback(new DriveFolder.OnCreateFileCallback() {
@Override
public void onCreateFile(DriveFolder.DriveFileResult driveFileResult) {
Log.d(TAG, "DriveID:"+driveFileResult.getDriveFile().getDriveId());
Log.d(TAG, "ResourceID:"+driveFileResult.getDriveFile().getDriveId().getResourceId());
}
});
}
});
}
输出是这样的:
DriveID:DriveId:CAESABiGAiCMlafg_VA=
资源ID:空
如果我关闭应用程序然后重新启动它,并列出文件夹的内容,我会得到正确的值:
驱动器ID:CAESHDBCeXR3by1ta0hDQ0JXRzAyWjA5UFIzUmlha0UYhAIgjJWn4P1Q
资源ID:0Bytwo-mkHCCBWG02Z09PR3RiakE
我尝试重新连接 GoogleApiClient、driveFileResult.getDriveFile().commitAndCloseContents()、DriveApi.requestSync()。 对我没有任何帮助。
如果有任何可能的帮助,我将不胜感激。
【问题讨论】:
-
如果您仍在寻找答案,stackoverflow.com/questions/21800257/… 有一些信息
-
同样的问题在这里要死我了
-
如果你明白了,请告诉我