关于爱情的问题createBitmap问题

& android bitmap oom 优化
android bitmap oom 优化
android使用位图显示图片,也就是像素点,jpg之类的压缩格式在android都会转成bitmap。
现在手机的分辨率也越来越高,480*800 大小的图片使用的内存大小:
480*800*32/8=.5M
32表示32位色,每个字节8位。
手机上有很多长图大小都是600*=24M,这样一来手机OOM是迟早的事。一些采用缩放和降低画质是解决不了问题的
例如下面这两种缩放还是会出现内存溢出的问题,
如何能让anroid获取网络图片时内存不OOM方法,使用BitmapFactory.decodeStream替代createBitmap方法,
原因是该方法直读取图片字节,调用JNI>>nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap。
android使用Matrix实现bitmap缩放
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int newWidth = 640;
int newHeight = 480;
float scaleWidth = ((float) newWidth) /
float scaleHeight = ((float) newHeight) /
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// create the new Bitmap object
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
android使用options.inJustDecodeBounds实现bitmap缩放
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =
BitmapFactory.decodeFile(path, options);
if (options.mCancel || options.outWidth == -1
|| options.outHeight == -1) {
Log.d(&OomDemo&, &alert!!!& + String.valueOf(options.mCancel)
+ & & + options.outWidth + options.outHeight);
options.inSampleSize = puteSampleSize(options, 600, (int) (1 * 1024 * 1024));
Log.d(&OomDemo&, &inSampleSize: & + options.inSampleSize);
options.inJustDecodeBounds =
options.inDither =
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
// 默认是Bitmap.Config.ARGB_8888
Bitmap bitmap = BitmapFactory.decodeFile(path, options);
//四种构造Bitmap的使用的字节数
Bitmap.createBitmap(width, height,Bitmap.Config.ALPHA_8);
Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_4444);
Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
Bitmap.createBitmap(width, height,Bitmap.Config.RGB_565);
public class Util {
* Compute the sample size as a function of minSideLength
* and maxNumOfPixels.
* minSideLength is used to specify that minimal width or height of a
* maxNumOfPixels is used to specify the maximal size in pixels that is
* tolerable in terms of memory usage.
* The function returns a sample size based on the constraints.
* Both size and minSideLength can be passed in as IImage.UNCONSTRAINED,
* which indicates no care of the corresponding constraint.
* The functions prefers returning a sample size that
* generates a smaller bitmap, unless minSideLength = IImage.UNCONSTRAINED.
* Also, the function rounds up the sample size to a power of 2 or multiple
* of 8 because BitmapFactory only honors sample size this way.
* For example, BitmapFactory downsamples an image by 2 even though the
* request is 3. So we round up the sample size to avoid OOM.
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedS
if (initialSize &= 8) {
roundedSize = 1;
while (roundedSize & initialSize) {
roundedSize &&= 1;
roundedSize = (initialSize + 7) / 8 * 8;
return roundedS
public static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outW
double h = options.outH
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 lowerB
if ((maxNumOfPixels == -1) &&
(minSideLength == -1)) {
} else if (minSideLength == -1) {
return lowerB
return upperB
android上屏幕密度和像素转换
//转换dip为px
public static int convertDIP2PX(Context context, int dip) {
float scale = context.getResources().getDisplayMetrics().
return (int)(dip*scale + 0.5f*(dip&=0?1:-1));
//转换px为dip
public static int convertPX2DIP(Context context, int px) {
float scale = context.getResources().getDisplayMetrics().
return (int)(px/scale + 0.5f*(px&=0?1:-1));
可以参考这里的实现方法
/Open-Source/Android/android-platform-apps/Gallery3D/com/cooliris/media/UriTexture.java.htm
除非注明,本站文章均为: 原创,转载请注明本文地址:
- 285,221 views - 223,173 views - 172,450 views - 134,913 views - 93,372 views - 86,460 views - 80,617 views - 79,629 views - 69,273 views - 58,650 views当前位置: &
求翻译:The proedure entry GdipCreateBitmapFromHBITMAP could not be loated in the dynamic link library gdiplus. dll是什么意思?
The proedure entry GdipCreateBitmapFromHBITMAP could not be loated in the dynamic link library gdiplus. dll
问题补充:
proedure条目GdipCreateBitmapFromHBITMAP不能在动态链接库gdiplus loated。 DLL
入境的proedure gdipcreatebitmapfromhbitmap不能loated在动态链接库Gdiplus. dll
proedure词条GdipCreateBitmapFromHBITMAP在动态连接库gdiplus不能是loated。 dll
② 条目 GdipCreateBitmapFromHBITMAP 无法在动态链接库 gdiplus loated。dll
proedure词条GdipCreateBitmapFromHBITMAP在动态连接库gdiplus不能是loated。 dll
我来回答:
参考资料:
* 验证码:
登录后回答可以获得积分奖励,并可以查看和管理所有的回答。 |
我要翻译和提问
请输入您需要翻译的文本!主题 : CGBitmapContextCreateImage(bitmap) 内存泄露问题处理
级别: 禁止发言
可可豆: 762 CB
威望: 762 点
在线时间: 175(时)
发自: Web Page
用户被禁言,该主题自动屏蔽!
级别: 骑士
可可豆: 2027 CB
威望: 2027 点
在线时间: 733(时)
发自: Web Page
回 楼主(zktc5418) 的帖子
所有CG***Create**方法都是要释放的
Your magic is mine.
级别: 圣骑士
可可豆: 3543 CB
威望: 3493 点
在线时间: 463(时)
发自: Web Page
mark一下。
级别: 新手上路
可可豆: 110 CB
威望: 110 点
在线时间: 129(时)
发自: Web Page
是的,用过经历!
级别: 新手上路
可可豆: 40 CB
威望: 40 点
在线时间: 39(时)
发自: Web Page
回 楼主(zktc5418) 的帖子
是的,要释放,今天也用到了。。。
级别: 骑士
可可豆: 2000 CB
威望: 2000 点
在线时间: 356(时)
发自: Web Page
mark一下!!
级别: 新手上路
可可豆: 3 CB
威望: 3 点
在线时间: 81(时)
发自: Web Page
mark一下 感谢分享!!🙏🙏
关注本帖(如果有新回复会站内信通知您)
iPhone5的分辨率? 正确答案:
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版出处:http://blog.csdn.net
尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,
因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存。
因此,改用先通过BitmapFactory.decodeStream方法,创建出一个bitmap,再将其设为ImageView的 source,
decodeStream最大的秘密在于其直接调用JNI&&nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap,从而节省了java层的空间。
如果在读取时加上图片的Config参数,可以跟有效减少加载的内存,从而跟有效阻止抛out of Memory异常
另外,decodeStream直接拿的图片来读取字节码了, 不会根据机器的各种分辨率来自动适应,
使用了decodeStream之后,需要在hdpi和mdpi,ldpi中配置相应的图片资源,
否则在不同分辨率机器上都是同样大小(像素点数量),显示出来的大小就不对了。
下面附上我个人写代码:
* Funciton:按指定长宽并以最省内存的方式读取本地资源的图片(通过C层API加载图片,消耗的是C层内存,而不会消耗JAVA虚拟机的内存)。
* &font color=&red&&【注意】此方法应该在以下非常情况下使用:应用内存非常吃紧或者图片过大时可使用此方法。
* 使用此方法的可能后果:会有一定几率造成应用崩溃,请慎用,&/font&
* @param context
* @param resId
public static Bitmap decodeStreamWithLowMemory(Context context, int resId,
int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds =
BitmapFactory.decodeResource(context.getResources(), resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds =
// Bitmap.Config.ALPHA_8,Bitmap.Config.ARGB_4444,Bitmap.Config.RGB_565
// 设置这几个参数效果都差不多,确实相当省内存啊,而且还跟原图清晰度相当
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inPurgeable =
options.inInputShareable =
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, options);
* Funciton:以最省内存的方式读取本地资源的图片; &font color=&red&&【注意】:
* 此方法以BitmapFactory.decodeResource ()去加载图片,使用此方法每次会创建一个新的Btimap对象,
* 而不会共享应用res中创建的Btimap资源,因此要即时回收旧图片,以免造成内存泄露。&/font&
* @param context
* @param resId
public static Bitmap decodeResourceWithLowMemory(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
// Bitmap.Config.ALPHA_8,Bitmap.Config.ARGB_4444,Bitmap.Config.RGB_565
// 设置这几个参数效果都差不多,确实相当省内存啊,而且还跟原图清晰度相当
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable =
opt.inInputShareable =
return BitmapFactory.decodeResource(context.getResources(), resId, opt);
作者:scry5566 发表于 16:16:51
阅读:4 评论:0
相关 [android 图片 内存] 推荐:
- 移动开发 - ITeye博客
在android开发过程当中,如果要用到大量图片而你又没有做好图片内存的回收,很容易就会造成OOM内存溢出的问题. 下面介绍一种图片内存回收的方法,就是将图片从添加到的ViewGroup中一张一张拿出来回收,具体代码如下:. 其中的ViewGroup可以是ViewFlipper、ViewPager等等继承自ViewGroup的子类.
- CSDN博客推荐文章
尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,. 因为这些函数在完成decode后,最终都是通过java层的createBitmap来完成的,需要消耗更多内存.
因此,改用先通过BitmapFactory.decodeStream方法,创建出一个bitmap,再将其设为ImageView的 source,.
- 博客园_首页
测试环境为Adnroid 2.1以上. 1:AndroidManifest.xml 权限配置:. 2.异步图片类 ImageDownloadTask . * 这里获取到手机的分辨率大小. 如:/xx.jpg. //这3句是处理图片溢出的begin( 如果不需要处理溢出直接 opts.inSampleSize=1;).
- CSDN博客推荐文章
这两天在搞个内存清理小插件,网上很少这方面资料,之前找过清理缓存的例子测试,结果不成功. 后来自己思考该怎么清理内存,于是成功搞出来了. 这两个方法是网上拷别人的,分别用来得到可用内存和内存总数,这样我们就可用得到了已用内存数. // 获取android当前可用内存大小. //mi.availM 当前系统的可用内存.
- CSDN博客推荐文章
内存泄漏引发很多问题:.
1:程序卡顿,响应速度慢(内存占用高时JVM 虚拟机会频繁出发GC).
2:莫名其妙消失. ANDROID 内存面临的问题.
1: 有限的堆内存,原始只有16M.
2:内存大小消耗等根据设备,操作系统等级,尺寸的不同而不同.
4:支持后台多任务处理.
- CSDN博客推荐文章
首先Android内存管理机制相当复杂,想要讲清楚比较困难;其次对于绝大多数用户来说,只关心内存够不够用,至于内存如何管理的这种技术细节,不是用户需要去考虑的,写这样一个专题有没有意义. 毕竟我们是用手机,不是来研究手机的. 最后的顾虑是这个专题会不会太技术化了,绝大部分用户不会看或者说缺乏相应的背景.
- CSDN博客移动开发推荐文章
1、引用没释放造成的内存泄露.
1.1、注册没取消造成的内存泄露.
Android的内存泄露比纯
Java的内存泄露还要严重,因为其他一些Android程序可能引用我们的Anroid程序的对象(比如注册机制). 即使我们的Android程序已经结束了,但是别的引用程序仍然还有对我们的Android程序的某个对象的引用,泄露的内存依然不能被垃圾回收.
- 博客园_首页
先来想一个问题:如果一个应用内存占用超过了16M,而手机系统分配的最大内存只有15M,那么会有什么问题. 没错,内存溢出,外在表现是手机卡死或者程序崩掉. 当然,Android有一套自己的垃圾回收机制,当一些被引用的对象不再使用的时候就当做“垃圾”来处理,用GC(垃圾处理器)释放掉被占用的那部分内存.
- CSDN博客移动开发推荐文章
相信一步步走过来的Android从业者,每个人都会遇到OOM的情况. 如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力. 今天我们就谈谈在Android平台下内存的管理之道,开始今天的主题之前,先再次回顾两个概念. 内存泄漏:对象在内存heap堆中中分配的空间,当不再使用或没有引用指向的情况下,仍不能被GC正常回收的情况.
- CSDN博客推荐文章
所有内容均来源于官方文档
/training/articles/memory.html. only way to completely release memory from your app is to release object references you may be holding, making the memory available to the garbage collector.
坚持分享优质有趣的原创文章,并保留作者信息和版权声明,任何问题请联系:@。}

我要回帖

更多关于 关于西游记的问题 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信