内存溢出应对:把图片固定大小赋值给bitmap
来源:互联网 发布:海湾编程软件 下载 编辑:程序博客网 时间:2024/05/16 09:26
主题://将绝对路径转换为Bitmap,动态计算inSampleSize限制图片大小此处大小为128*128 RGB_565(默认为8888)下为 2.7K public static Bitmap getFixedSizeImg(String filePath){ Bitmap bitmap = null; BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.RGB_565; opts.inPurgeable = true; opts.inInputShareable = true; BitmapFactory.decodeFile(filePath, opts); opts.inSampleSize = computeSampleSize(opts, -1, 128*128); opts.inJustDecodeBounds = false; try { bitmap = BitmapFactory.decodeFile(filePath, opts); }catch (Exception e) { // TODO: handle exception } return bitmap; }//计算inSampleSize 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; }//计算inSampleSize private static int computeInitialSampleSize(BitmapFactory.Options options,int minSideLength, int maxNumOfPixels) { double w = options.outWidth; double h = options.outHeight; 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; } }主题结束重写Activity此方法可查看app内存状态@Overridepublic void onTrimMemory(int level) { super.onTrimMemory(level); switch (level) { case TRIM_MEMORY_RUNNING_MODERATE: LongLog.loge("你的app正在运行并且不会被列为可杀死的"); break; case TRIM_MEMORY_RUNNING_LOW: LongLog.loge("你的app正在运行且没有被列为可杀死的"); break; case TRIM_MEMORY_RUNNING_CRITICAL: LongLog.loge("你的app仍在运行,但是系统已经把LRU Cache中的大多数进程都已经杀死"); break; case TRIM_MEMORY_BACKGROUND: LongLog.loge("系统正运行于低内存"); break; case TRIM_MEMORY_MODERATE: LongLog.loge(" 系统正运行于低内存状态并且你的进程已经已经接近LRU名单的中部位置"); break; case TRIM_MEMORY_COMPLETE: LongLog.loge("系统正运行与低内存的状态并且你的进程正处于LRU名单中最容易被杀掉的位置"); break; }}
阅读全文
0 0
- 内存溢出应对:把图片固定大小赋值给bitmap
- Bitmap获取图片内存溢出
- Android Bitmap图片处理,防止内存溢出
- 加载图片Bitmap时内存溢出/压缩图片
- android bitmap内存溢出
- bitmap时候内存溢出
- bitmap 内存溢出
- Android bitmap 内存溢出
- Bitmap内存溢出问题
- android bitmap内存溢出
- Android Bitmap内存溢出
- Bitmap 内存溢出
- Android Bitmap内存溢出
- android Bitmap内存溢出
- android bitmap设置图片内存溢出的解决神器
- 把Bitmap中的图片保存至内存块
- bitmap内存溢出的问题
- Android Bitmap 内存溢出解决方法
- 《数字技术》连载24: 第3章 逻辑设计初步 第4节 几种观点
- JVM GC垃圾回收器详解
- Kotlin简单网络请求及数据解析
- src/tcmalloc.cc:284] Attempt to free invalid pointer 0x7ff4821267d0 caffe VideoCapture Mac
- 正则表达式
- 内存溢出应对:把图片固定大小赋值给bitmap
- c++11 中的 move 与 forward
- 网卡驱动程序实践
- Java | 向下转型与运行时类型识别
- 产品经理内功修炼——"动起来"的产品
- 反卷积网络在文本表征方面的应用
- Java:String、StringBuffer和StringBuilder的区别
- Java集合框架
- 爬取酷狗top500