问题
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
The docs say that a reference to a Snapshot has to be obtained before calling writeSnaphot. Since snapshot is an interface it can't be created with new.
How to obtain a reference to a Snapshot?
Thank You!
P.S. I see that there is a way to obtain a reference by opening an existing saved game by name however the reference I would like to obtain is for a new Snapshot there are currently no existing snapshots so using the load function probably will not succeed in writing a new snapshot.
回答1:
You can call open with a filename that does not exist to create a new snapshot. This snippet uses .await() on the result from open, so you'll need to call it from an AsyncTask or some other non-UI thread. (see https://developers.google.com/games/services/android/savedgames for more details):
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;
}
来源:https://stackoverflow.com/questions/29969656/obtaining-a-reference-to-a-snapshot-google-play-games-services-saved-games