package cn.hugo.android.scanner.common; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class BitmapUtils { public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options); // Calculate inSampleSize options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); } public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { // Calculate ratios of height and width to requested height and // width final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); // Choose the smallest ratio as inSampleSize value, this will // guarantee // a final image with both dimensions larger than or equal to the // requested height and width. inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } public static Bitmap getCompressedBitmap(String path) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); options.inSampleSize = calculateInSampleSize(options, 480, 800); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(path, options); } }
相关推荐
图片oom,解决方法 图片oom,完美deom
安卓下显示图片经常遇到OOM问题,这里给出了常用解决方案
Android解决图片加载OOM与listview图片错位乱跳问题解决,开源的代码,里面有示例demo
本Demo包含2个.JAVA文件,一个图片OOM处理工具类,一个MainACtivity类,通过一个Button打开手机所以相册,然后选择图片经过OOM处理,将处理后的图片显示到ImageView上。 【简单易懂,含详细注释(开发版本:安卓4.4.2...
这个demo解决了由于大量使用大图片,导致极度不流畅,甚至OOM。使用了二次裁剪,进行图片压缩,减小内存的使用
加载大图片到内存时如何防止 OOM的产生
解决Android加载图片出现的OOM问题
安卓在加载大图的时候经常会出现oom的错误,给大家分享我的一些处理经验。
android gridview 加载大量图片。无OOM。 程序运行默认加载SD卡内所有图片文件,这是我写了之后用在一款成熟app里面的。可用
android加载大图避免oom,博客http://blog.csdn.net/u012305710/article/details/51079676
挑战11张8k分辨率大图,共603MB大小连续进行压缩不OOM 博客介绍地址:http://blog.csdn.net/junjun071308/article/details/48606673
防止下载图片的过程中产生OOM,当滚动的过程中停止下载!
ViewPager加载大量图片oom解决方案
android图片墙,解决oom 博客地址: http://blog.csdn.net/pangzaifei/article/details/37763753
android Gallery 3d 图片浏览 倒影 oom解决了 读取可获取内置外置sd卡中图片
问题原因分析:使用ScriptEngine.eval每次都会对脚本进行编译,生成一个新的类,被GroovyClassLoader加载,大量执行计算后,将导致被加载的类数量不断增加,最终OOM。 解决办法:对计算的表达式expression进行预...
本例主要介绍导入从src导入图片资源到应用,防止OOM,对图片做放大缩小等,可以学习代码的写法,下载可以直接运行使用
android 图片下载 防止OOM
安卓图片压缩类,避免内存溢出OOM。 利用安卓自身的计算方法。
从项目拷出来的源码,解决ViewFlipper加载多张图片出现OOM问题,测试加载20张图片无异常,有需要的同学可以下载看看,需自己修改下代码