欢迎指出,万分感谢。只为相互学习和进步。如果能对您有所帮助或者启发,便是我最开心的事。
第三部分:图片的内存溢出
上次说完 图片的压缩和缩略图 这次主要说 图片显示的时候 内存溢出的问题
显示Bitmap之前 会经常遇到一些内存溢出的情况 这是因为imageView调用一些显示方法的时候 占用的内存过多 不是说 一张压缩后20K的图片 就一定占用的内存小。 所以在显示尺寸大的R.drawable.XXX的时候可调用下面方法
/** * 大图片处理机制 * 利用Bitmap 转存 R图片 */ public static Bitmap btp; public static void getBitmapForImgResourse(Context mContext, int imgId, ImageView mImageView) throws IOException { InputStream is = mContext.getResources().openRawResource(imgId); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; options.inSampleSize = 1; btp = BitmapFactory.decodeStream(is, null, options); mImageView.setImageBitmap(btp); // btp.recycle(); is.close(); }
说明option中的一些方法
public Bitmap revitionImageSize(String path) throws IOException { InputStream is = new FileInputStream(path); BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; //2.为位图设置10K的缓存 options.inTempStorage = new byte[10*1024]; //3.设置位图颜色显示优化方式 options.inPreferredConfig = Bitmap.Config.RGB_565; //4.设置图片可以被回收,创建Bitmap用于存储Pixel的内存空间在系统内存不足时可以被回收 options.inPurgeable = true; //6.设置解码位图的尺寸信息 options.inInputShareable = true; //5.设置位图缩放比例 options.inSampleSize = 4; BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; Bitmap bitmap = null; while (true) { if ((options.outWidth >> i <= 256) && (options.outHeight >> i <= 256)) { in = new BufferedInputStream( new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } return bitmap; }在此只抛砖引玉,简略介绍思路,因为图像处理内容繁杂,希望各位能有所启发,找到研究的方向。然后再深入的去了解。早日解决遇到的问题。后续也会继续补充关于图像的帖子。