/**
* Created by kingadmin on 2018/4/17.
*/
class BitmapUtil private constructor(private val context: Context) {
/**
* 文件转Bitmap
*/
fun fileToBitmap(filePath: String): Bitmap {
val file = File(filePath)
val options = BitmapFactory.Options()
/**
* 压缩长宽各为一半避免图片过大装载不了
*/
options.inPurgeable = true
options.inSampleSize = 2
return BitmapFactory.decodeFile(filePath, options)
}
/**
* Bitmap转文件
*/
fun bitmapToFile(bitmap: Bitmap, saveFilePath: String): File? {
val file = File(saveFilePath)//将要保存图片的路径
try {
val bos = BufferedOutputStream(FileOutputStream(file))
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos)
bos.flush()
bos.close()
return file
} catch (e: IOException) {
e.printStackTrace()
return null
}
Android Bitmap最全基本操作:与Drawable 、File、btye[]互转(Kotlin 版)
本文详细介绍了在Android环境中使用Kotlin进行Bitmap操作,包括如何将Bitmap转换为Drawable、File和byte数组,以及如何从这些格式反向转换回Bitmap,是Android开发者不可或缺的Bitmap操作参考。
摘要由CSDN通过智能技术生成