BitmapFactory

本文详细介绍了Bitmap类的11种创建方法,包括从字节数组、文件路径、资源文件等不同来源创建位图的方式,并对比了不同方法的内存消耗情况,推荐使用直接操作底层内存的方法来避免内存溢出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个类比较简单、通过不同来源创建位图 差不多11个方法,
  • decodeByteArray(byte[] data, int offset, int length,BitmapFactory.Options opts)
    从一个data字节数组中解析图片,从offset下标开始,
  • decodeByteArray(byte[] data, int offset, int length)
    同上,无opts参数
  • decodeFile(String pathName)
    从一个pathName路径解析图片

  • decodeFile(String pathName, BitmapFactory.Options opts)
    同上,包含opts

  • decodeFileDescriptor(FileDescriptor fd)

  • decodeFileDescriptor(FileDescriptor fd, Rect outPadding,BitmapFactory.Options opts)
  • decodeResource(Resources res, int id, BitmapFactory.Options opts)
    从android资源文件夹解析图片

  • decodeResource(Resources res, int id)
    同上,不包含opts参数

  • decodeResourceStream(Resources res, TypedValue value, InputStream is,
    Rect pad, BitmapFactory.Options opts)

  • decodeStream(InputStream is)
    从is流中解析图片

  • decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
    同上

如上生成位图的方法decodeResource..(…)这种不建议使用,因为种方生成位图时会在java层做一些操作,这样内存消耗更多,而decodeFile(。。。)方法直接操作底层内存消耗更少,防止OOM这个是非常关键的一步。使用时注意查看源码非常清楚二者的使用区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值