首页 > 解决方案 > 适用于 Android 的 Google Drive API - CreateFile 成功,但没有文件发送到云端

问题描述

我正在将我的 Xamarin.Android 应用程序与 Google Drive API 集成。我的主要问题是,即使所有 API 调用最终都成功,我的一些二进制文件也没有保存在 Google Drive 中。

我尝试发送 5-10 个大小为 10-80 MB 的音频文件(wav、mp3、ogg 等)。这是我发送二进制文件的方法:

                string mimeType = GetMimeType(localFilePath);
                string fullSourceFilePath = Path.Combine(documentsPath, localFilePath);
                byte[] buffer = new byte[BinaryStreamsBufferLength];

                using (var contentResults = await DriveClass.DriveApi.NewDriveContentsAsync(_googleApiClient).ConfigureAwait(false))
                {
                    if (contentResults.Status.IsSuccess) //always returns true
                    {
                        using (var readStream = System.IO.File.OpenRead(fullSourceFilePath))
                        {
                            using (var fileChangeSet = new MetadataChangeSet.Builder()
                               .SetTitle(path[1])
                               .SetMimeType(mimeType)
                               .Build())
                            {
                                using (var writeStream = new BinaryWriter(contentResults.DriveContents.OutputStream))
                                {
                                    int bytesRead = readStream.Read(buffer, 0, BinaryStreamsBufferLength);

                                    while (bytesRead > 0)
                                    {
                                        writeStream.Write(buffer, 0, bytesRead);
                                        bytesRead = readStream.Read(buffer, 0, BinaryStreamsBufferLength);
                                    }

                                    writeStream.Close();
                                }

                                readStream.Close();

                                var singleFileCopyResult = await targetCloudFolder.CreateFileAsync(_googleApiClient, fileChangeSet, contentResults.DriveContents,
                                    new ExecutionOptions.Builder().SetConflictStrategy(ExecutionOptions.ConflictStrategyOverwriteRemote).Build()).ConfigureAwait(false);

                                return singleFileCopyResult.Status.IsSuccess; //always returns true
                            }
                        }
                    }
                }

上述方法在 foreach 循环中调用,其中包含需要发送的本地存储文件列表。

另外,第一次连接 API 时,我请求同步:

if (_isSyncRequired)
            syncResult = await DriveClass.DriveApi.RequestSyncAsync(_googleApiClient).ConfigureAwait(false);

经过几天尝试各种解决方案,我已经在尝试各种解决方案,包括: - 更改流类型 - 更改 API 方法调用的顺序 - 使用非异步 API 方法(实际上也是异步的) - 尽可能尝试不同的重载和参数 - 尝试重构为非过时的 API 调用(不可能这样做,因为它需要具有大量依赖项的超重 GoogleSignIn NuGet 并且我的应用程序足够重)

以上都不起作用,我的文件后来被成功下载,大约有 80% 的文件被发送(所有上传状态都是成功的)。

到目前为止,你们中是否有人遇到过类似的问题?如果所有上传方法都声称一切成功,您有什么提示可以使文件可下载吗?

非常感谢您提供任何线索:)

标签: c#androidxamarinxamarin.androidgoogle-drive-android-api

解决方案


推荐阅读