获取对快照的引用。 Google Play 游戏服务保存的游戏

Obtaining a reference to a Snapshot. Google Play Games Services Saved Games

       private PendingResult<Snapshots.CommitSnapshotResult> writeSnapshot(Snapshot snapshot,
                byte[] data, Bitmap coverImage, String desc) {

            // Set the data payload for the snapshot
            snapshot.getSnapshotContents().writeBytes(data);

            // Create the change operation
            SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder()
                    .setCoverImage(coverImage)
                    .setDescription(desc)
                    .build();

            // Commit the operation
            return Games.Snapshots.commitAndClose(mGoogleApiClient, snapshot, metadataChange);
        }

https://developers.google.com/games/services/android/savedgames

文档说在调用 writeSnaphot 之前必须获得对快照的引用。由于快照是一个接口,因此无法使用 new 创建它。

如何获取对快照的引用?

谢谢!

P.S。我看到有一种方法可以通过按名称打开现有的已保存游戏来获取参考,但是我想获得的参考是针对新快照的,目前没有现有快照,因此使用加载功能可能无法成功编写新快照。

您可以使用不存在的文件名调用打开来创建新的快照。此代码段对打开的结果使用 .await(),因此您需要从 AsyncTask 或其他一些非 UI 线程调用它。 (有关详细信息,请参阅 https://developers.google.com/games/services/android/savedgames):

private PendingResult<Snapshots.CommitSnapshotResult> writeSnapshot(String newSnapshotFilename,
       byte[] data, Bitmap coverImage, String desc) {

Snapshots.OpenSnapshotResult result =
   Games.Snapshots.open(mGoogleApiClient, newSnapshotFilename, true).await();
// Check the result of the open operation
  if (result.getStatus().isSuccess()) {
    Snapshot snapshot = result.getSnapshot();
    snapshot.getSnapshotContents().writeBytes(data);

    // Create the change operation
    SnapshotMetadataChange metadataChange = new
           SnapshotMetadataChange.Builder()
                .setCoverImage(coverImage)
                .setDescription(desc)
                .build();

    // Commit the operation
   return Games.Snapshots.commitAndClose(mGoogleApiClient, snapshot, metadataChange);
   }
   return null;
}