【发布时间】:2020-02-18 13:53:42
【问题描述】:
我正在尝试创建一个帮助程序类,该类将使用 Kotlin 在我的 android 应用程序中处理读取和写入内部文件。
以下是我按顺序访问的链接:
- https://developer.android.com/guide/topics/data/data-storage
- https://developer.android.com/training/data-storage/files.html#WriteInternalStorage
- https://developer.android.com/training/data-storage/files/internal
- https://developer.android.com/topic/security/data
这是我的代码:
package com.example.testapp
// Added
import android.content.Context
import java.io.File
// External
import androidx.security.crypto.EncryptedFile
import androidx.security.crypto.MasterKeys
@Suppress("unused")
class SystemMain {
fun writeFile(context: Context) {
// Although you can define your own key generation parameter specification, it's recommended that you use the value specified here.
val keyGenParameterSpec = MasterKeys.AES256_GCM_SPEC
val masterKeyAlias = MasterKeys.getOrCreate(keyGenParameterSpec)
// Creates a file with this name, or replaces an existing file that has the same name. Note that the file name cannot contain path separators.
val fileToWrite = "my_sensitive_data.txt"
val encryptedFile = EncryptedFile.Builder(File(fileToWrite), context, masterKeyAlias, EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB).build()
encryptedFile.bufferedWriter().use { writer ->
writer.write("MY SUPER-SECRET INFORMATION")
}
}
}
这是我的 build.gradle:
...
implementation "androidx.lifecycle:lifecycle-extensions:2.1.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.1.0"
implementation "androidx.security:security-crypto:1.0.0-alpha02"
...
我得到的错误在这里:encryptedFile.bufferedWriter()
未解决的参考。以下候选人均不适用 因为接收器类型不匹配:
- @InlineOnly public inline fun File.bufferedWriter(charset: Charset = ..., bufferSize: Int = ...): kotlin.io 中定义的缓冲写入器
- @InlineOnly public inline fun OutputStream.bufferedWriter(charset: Charset = ...): kotlin.io 中定义的BufferedWriter
我是否在某处遗漏了参考资料?我是否使用了不正确的参考文献?代码是否已更改,链接上的文档是否已过时?
我对此很陌生。任何建议/帮助将不胜感激。
【问题讨论】:
-
异或可能有帮助请参考此链接stackoverflow.com/a/44823351/3615605
-
感谢@MathaN,但我如何在我的示例中实现这一点?
标签: android kotlin internal-storage android-internal-storage