使用 Kotlin 将文件保存path
到tmpPath
:
Files.newInputStream(path).use { inputStream ->
Files.newOutputStream(tmpPath).use { tmpOutputStream ->
BitmapFactory
.decodeStream(inputStream)
.compress(Bitmap.CompressFormat.JPEG, 30, tmpOutputStream)
}
}
编辑:确保检查解码失败(并返回 null)的可能性以及压缩实际工作的可能性(布尔返回类型)。
val success: Boolean = Files.newInputStream(path).use { inputStream ->
Files.newOutputStream(tmpPath).use { tmpOutputStream ->
BitmapFactory
.decodeStream(inputStream)
?.compress(Bitmap.CompressFormat.JPEG, config.qualityLevel, tmpOutputStream)
?: throw Exception("Failed to decode image")
}
}
if (!success) {
throw Exception("Failed to compress and save image")
}