紧接上篇博文 , 在获取拍照拍下的图片并将其设为背景的时候出现了问题spa
系统给出了这样的警告:.net
Bitmap too large to be uploaded into a texture
意思很明显:就是如今的手机硬件愈来愈强悍了(-.-||).拍下来的图片太大,没法直接展现.code
网上有说关闭硬件加速喝分片加载的办法.不过这都不是我想要的.这种问题天然是要压缩图片的orm
嘛!!htm
压缩代码:blog
/** * 图片质量压缩 * **/ private Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while ( baos.toByteArray().length / 1024>100) { //循环判断若是压缩后图片是否大于100kb,大于继续压缩 baos.reset();//重置baos即清空baos options -= 10;//每次都减小10 image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片 return bitmap; } /** * 图片比例压缩 * */ private Bitmap getimage(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(srcPath,newOpts);//此时返回bm为空 newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; //如今主流手机比较可能是800*480分辨率,因此高和宽咱们设置为 float hh = 800f;//这里设置高度为800f float ww = 480f;//这里设置宽度为480f //缩放比。因为是固定比例缩放,只用高或者宽其中一个数据进行计算便可 int be = 1;//be=1表示不缩放 if (w > h && w > ww) {//若是宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {//若是高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置缩放比例 //从新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, newOpts); return compressImage(bitmap);//压缩比如例大小后再进行质量压缩 }
这是脚本之家的大神写的拿来膜拜.图片
大神连接:http://www.jb51.net/article/41117.htmget