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

Eae*_*Eae 2 google-play-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);
        }
Run Code Online (Sandbox Code Playgroud)

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

文档说在调用writeSnaphot之前必须获取对Snapshot的引用.由于快照是一个界面,因此无法使用new创建.

如何获取对快照的引用?

谢谢!

PS我看到有一种方法可以通过按名称打开现有的已保存游戏来获取引用但是我想要获得的引用是针对新的快照,目前没有现有的快照,因此使用load函数可能无法成功写入一个新的快照.

Cla*_*son 7

您可以使用不存在的文件名调用open来创建新快照.此代码段对open的结果使用.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;
}
Run Code Online (Sandbox Code Playgroud)