android BitmapFactroy

本文详细介绍了BitmapFactory.Options类中的关键属性及其使用方法,包括inJustDecodeBounds、inPreferredConfig、inPurgeable、inInputShareable及inSampleSize等,帮助开发者有效控制Bitmap的加载过程,减少内存占用。

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

一、
BitmapFactory.Options内部变量意义和使用方法:

1、inJustDecodeBounds

在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。怎么避免它呢?
如果我们把inJustDecodeBounds设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。

使用方法:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(path, options);/* 这里返回的bmp是null */
这段代码之后,options.outWidth 和 options.outHeight就是我们想要的宽和高了。

2.options.inPreferredConfig

BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;    // 默认是Bitmap.Config.ARGB_8888

3.

/* 下面两个字段需要组合使用 */
options.inPurgeable = true;
options.inInputShareable = true;
Bitmap bmp = BitmapFactory.decodeFile(path, options);

inPurgeable 是控制 Bitmap 对象是否使用软引用机制, 在系统需要的时候可以回收该对象, 如果在此后, 程序又需要使用该对象, 则系统重新 decode 该对象.
inInputShareable 和 inPurgeable 组合使用的, 是控制是否复制 inputfile 对象的引用, 如果不复制, 那么要实现 inPurgeable 机制就需要复制一份 file 数据, 才能在系统需要 decode 的时候创建一个 bitmap 对象.

4.
Options.inSampleSize,一般用户获取图片的缩略图
inSampleSize表示缩略图大小为原始图片大小的几分之一,若该值为3,则取出的缩略图的宽和高都是原始图片的1/3,图片大小就为原始大小的1/9
举例,获取原图大小为625x690 90.2kB 图片的缩略图,尺寸为100X200,代码如下:

//获取图片的缩略图 
private Bitmap getBitmapThumbnail(String filePath){ 
BitmapFactory.Options options=new BitmapFactory.Options(); 
//true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息 
options.inJustDecodeBounds=true; 
//此时rawBitmap为null 
Bitmap rawBitmap = BitmapFactory.decodeFile(filePath, options); 
if (rawBitmap==null) { 
System.out.println("此时rawBitmap为null"); 
} 
//计算sampleSize 
int sampleSize=computeSampleSize(options, 100, 100*200); 
//为了读到图片,必须把options.inJustDecodeBounds设回false 
options.inJustDecodeBounds = false; 
options.inSampleSize = sampleSize; 

**//原图大小为625x690 90.2kB 
//测试调用computeSampleSize(options, 100, 200*100); 
//得到sampleSize=8 
//得到宽和高位79和87** 
//79*8=632 87*8=696 

Bitmap thumbnailBitmap=BitmapFactory.decodeFile(filePath, options); 
//保存到SD卡方便比较 
this.compressAndSaveBitmapToSDCard(thumbnailBitmap, "15.jpg", 80); 
return thumbnailBitmap; 
}

//第一个参数:原本Bitmap的options 
//第二个参数:希望生成的缩略图的宽高中的较小的值 
//第三个参数:希望生成的缩量图的总像素 
public static int computeSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 

int initialSize = computeInitialSampleSize(options, minSideLength,maxNumOfPixels); 
int roundedSize; 
if (initialSize <= 8) { 
roundedSize = 1; 
while (roundedSize < initialSize) { 
roundedSize <<= 1; 
} 
} else { 
roundedSize = (initialSize + 7) / 8 * 8; 
} 
return roundedSize; 
} 

private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { 
//原始图片的宽 
double w = options.outWidth; 
//原始图片的高 
double h = options.outHeight; 

System.out.println("========== w="+w+",h="+h); 

int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math 
.sqrt(w * h / maxNumOfPixels)); 

int upperBound = (minSideLength == -1) ? 128 : (int) Math.min( 
Math.floor(w / minSideLength), Math.floor(h / minSideLength)); 
if (upperBound < lowerBound) { 
// return the larger one when there is no overlapping zone. 
return lowerBound; 
} 
if ((maxNumOfPixels == -1) && (minSideLength == -1)) { 
return 1; 
} else if (minSideLength == -1) { 
return lowerBound; 
} else { 
return upperBound; 
} 
} 

//压缩且保存图片到SDCard 
private void compressAndSaveBitmapToSDCard(Bitmap rawBitmap,String fileName,int quality){ 
String saveFilePaht=this.getSDCardPath()+File.separator+fileName; 
File saveFile=new File(saveFilePaht); 
if (!saveFile.exists()) { 
try { 
saveFile.createNewFile(); 
FileOutputStream fileOutputStream=new FileOutputStream(saveFile); 
if (fileOutputStream!=null) { 
//imageBitmap.compress(format, quality, stream); 
//把位图的压缩信息写入到一个指定的输出流中 
//第一个参数format为压缩的格式 
//第二个参数quality为图像压缩比的值,0-100.0 意味着小尺寸压缩,100意味着高质量压缩 
//第三个参数stream为输出流 
rawBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream); 
} 
fileOutputStream.flush(); 
fileOutputStream.close(); 
} catch (IOException e) { 
e.printStackTrace(); 
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值