Avoid crash in encryptToUri has no write access to URI

This commit is contained in:
Juha Heinanen
2026-04-21 08:34:02 +03:00
parent e0e83a3195
commit 493703fad4

View File

@ -710,10 +710,15 @@ object Utils {
fun encryptToUri(ctx: Context, uri: Uri, content: ByteArray, password: String): Boolean { fun encryptToUri(ctx: Context, uri: Uri, content: ByteArray, password: String): Boolean {
val obj = encrypt(content, password.toCharArray()) val obj = encrypt(content, password.toCharArray())
val stream = ctx.contentResolver.openOutputStream(uri) as FileOutputStream
try { try {
ObjectOutputStream(stream).use { val stream = ctx.contentResolver.openOutputStream(uri)
it.writeObject(obj) if (stream != null)
ObjectOutputStream(stream).use {
it.writeObject(obj)
}
else {
Log.w(TAG, "encryptToUri: could not open output stream")
return false
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.w(TAG, "encryptToUri failed: $e") Log.w(TAG, "encryptToUri failed: $e")