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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...