在这篇文章中,我们分析了Android在对大图处理时的一些策略——Android异步加载全解析之大图处理 戳我戳我
那么在这篇中,我们来对图像——Bitmap进行一个更加细致的分析,掌握Bitmap的点点滴滴。
URL url = new URL(urlString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); is = new BufferedInputStream(conn.getInputStream()); bitmap = BitmapFactory.decodeStream(is);
FileInputStream is = = new FileInputStream(path); bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);因此,在调用DiskLruCache来使用的时候,这个方法就可以比decodeFile更快。这里我们也提供一个完整的调用代码:
public static OutputStream decodeBitmap(String path) { BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true;// 设置成了true,不占用内存,只获取bitmap宽高 BitmapFactory.decodeFile(path, opts); opts.inSampleSize = computeSampleSize(opts, -1, 1024 * 800); opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true opts.inPurgeable = true; opts.inInputShareable = true; opts.inDither = false; opts.inPurgeable = true; opts.inTempStorage = new byte[16 * 1024]; FileInputStream is = null; Bitmap bmp = null; InputStream ins = null; ByteArrayOutputStream baos = null; try { is = new FileInputStream(path); bmp = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts); double scale = getScaling(opts.outWidth * opts.outHeight, 1024 * 600); Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, (int) (opts.outWidth * scale), (int) (opts.outHeight * scale), true); bmp.recycle(); baos = new ByteArrayOutputStream(); bmp2.compress(Bitmap.CompressFormat.JPEG, 100, baos); bmp2.recycle(); return baos; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { is.close(); ins.close(); baos.close(); } catch (IOException e) { e.printStackTrace(); } System.gc(); } return baos; } private static double getScaling(int src, int des) { /** * 目标尺寸÷原尺寸 sqrt开方,得出宽高百分比 */ double scale = Math.sqrt((double) des / (double) src); return scale; }
private Bitmap compressImage(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); //质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); int options = 100; //循环判断如果压缩后图片是否大于100kb,大于继续压缩 while (baos.toByteArray().length / 1024 > 100) { //重置baos即清空baos baos.reset(); //这里压缩options%,把压缩后的数据存放到baos中 bitmap.compress(Bitmap.CompressFormat.JPEG, options, baos); //每次都减少10 options -= 10; } //把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); //把ByteArrayInputStream数据生成图片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; }
原文:http://blog.csdn.net/eclipsexys/article/details/44804101