如何解决Google游戏服务Saved Games SnapshotContents.readFully返回空字符串
在commitAndClose()成功说完后,我一直坚持要保存游戏内容(总是返回一个空字符串)。
我的代码如下:
// Write game data
val lastAccount = GoogleSignIn.getLastSignedInAccount(context) ?: throw RuntimeException("account is null")
val snapshotsClient = Games.getSnapshotsClient(context,lastAccount)
val snapshotTask = snapshotsClient?.open(fileName,true,RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED)
snapshotTask?.addOnFailureListener {
throw it
}?.addOnCompleteListener {
val snapshot = it.result?.data
if (snapshot != null) {
snapshot.snapshotContents.writeBytes(data.toByteArray(Charsets.UTF_8))
// Create the change operation
val metadataChange = SnapshotMetadataChange.Builder()
.setCoverImage(coverImage)
.setDescription(type)
.build()
// Commit the operation
snapshotsClient.commitAndClose(snapshot,metadataChange)?.addOnCompleteListener {
// it.isSuccessful is true here
}
}
}
// Fetch game data
val lastAccount = GoogleSignIn.getLastSignedInAccount(context) ?: throw RuntimeException("account is null")
val snapshotsClient = Games.getSnapshotsClient(context,RESOLUTION_POLICY_MOST_RECENTLY_MODIFIED)
snapshotTask?.addOnFailureListener {
listener.onFail(it)
}?.continueWith { task ->
val snapshot: Snapshot? = task.result?.data
// Opening the snapshot was a success and any conflicts have been resolved.
try { // Extract the raw data from the snapshot.
val string = snapshot?.snapshotContents?.readFully()?.toString(Charsets.UTF_8)
// string here always get ''
// ...
} catch (e: IOException) {
listener.onFail(e)
}
}
非常感谢您的帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。