Android Bitmap最全基本操作:与Drawable 、File、btye[]互转(Kotlin 版)

本文详细介绍了在Android环境中使用Kotlin进行Bitmap操作,包括如何将Bitmap转换为Drawable、File和byte数组,以及如何从这些格式反向转换回Bitmap,是Android开发者不可或缺的Bitmap操作参考。
摘要由CSDN通过智能技术生成
/**
 * 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
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值