/** * decode这个图片并且按比例缩放以减少内存消耗,虚拟机对每张图片的缓存大小也是有限制的 * * @param f * @param requiredSize * @return */ private Bitmap decodeFile(File f, int requiredSize) { try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, o); // 计算合理的缩放指数(2的整幂数) int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { // if (width_tmp / 2 < requiredSize || height_tmp / 2 < // requiredSize){ // break; // } // width_tmp /= 2; // height_tmp /= 2; // scale *= 2; if (width_tmp <= requiredSize || height_tmp <= requiredSize) { break; } width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); // o2.inSampleSize = (int) scale; o2.inSampleSize = scale; o2.inPreferredConfig = config.getBitmapConfig(); return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) { } return null; }