android使用位图显示图片,也就是像素点,jpg之类的压缩格式在android都会转成bitmap。

现在手机的分辨率也越来越高,480*800 大小的图片使用的内存大小:

480*800*32/8=1536000 =1.5M

32表示32位色,每个字节8位。

手机上有很多长图大小都是600*10000*32/8=24M,这样一来手机OOM是迟早的事。一些采用缩放和降低画质是解决不了问题的

例如下面这两种缩放还是会出现内存溢出的问题,

如何能让anroid获取网络图片时内存不OOM方法,使用BitmapFactory.decodeStream替代createBitmap方法,

原因是该方法直读取图片字节,调用JNI>>nativeDecodeAsset()来完成decode,无需再使用java层的createBitmap。

android使用Matrix实现bitmap缩放

[java]

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) / width;

float scaleHeight = ((float) newHeight) / height;

Matrix matrix = new Matrix();

matrix.postScale(scaleWidth, scaleHeight);

// create the new Bitmap object

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

[/java]

android使用options.inJustDecodeBounds实现bitmap缩放

[java]

BitmapFactory.Options options = new BitmapFactory.Options();

options.inJustDecodeBounds = true;

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);

return null;

}

options.inSampleSize = Util.computeSampleSize(options, 600, (int) (1 * 1024 * 1024));

Log.d("OomDemo", "inSampleSize: " + options.inSampleSize);

options.inJustDecodeBounds = false;

options.inDither = false;

options.inPreferredConfig = Bitmap.Config.ARGB_8888; // 默认是Bitmap.Config.ARGB_8888

Bitmap bitmap = BitmapFactory.decodeFile(path, options);

[/java]

//四种构造Bitmap的使用的字节数

Bitmap.createBitmap(width, height,Bitmap.Config.ALPHA_8); 8bit

Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_4444); 12bit

Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888); 32bit

Bitmap.createBitmap(width, height,Bitmap.Config.RGB_565); 16bit

[java]

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

* bitmap.

* 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 roundedSize;

if (initialSize <= 8) {

roundedSize = 1;

while (roundedSize < initialSize) {

roundedSize <<= 1;

}

} else {

roundedSize = (initialSize + 7) / 8 * 8;

}

return roundedSize;

}

public 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;

}

}

}

[/java]

android上屏幕密度和像素转换

[java]

//转换dip为px

public static int convertDIP2PX(Context context, int dip) {

float scale = context.getResources().getDisplayMetrics().density;

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().density;

return (int)(px/scale + 0.5f*(px>=0?1:-1));

}

[/java]

可以参考这里的实现方法

http://www.java2s.com/Open-Source/Android/android-platform-apps/Gallery3D/com/cooliris/media/UriTexture.java.htm

android bitmap.createbitmap内存溢出,android bitmap oom 优化相关推荐

  1. android 动画 图片 内存溢出,Android有效解决加载大图片时内存溢出的问题

    尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图, 因为这些函数在完成decode后,最终都是通过ja ...

  2. android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法

    android通过BitmapFactory.decodeFile获取图片bitmap报内存溢出的解决办法 原方法: public static Bitmap getSmallBitmap(Strin ...

  3. Android 内存溢出解决方案(OOM)

    众所周知,每个Android应用程序在运行时都有一定的内存限制,限制大小一般为16MB或24MB(视平台而定).因此在开发应用时需要特别关注自身的内存使用量,而一般最耗内存量的资源,一般是图片.音频文 ...

  4. android转base64内存溢出,base64编码处理大文件

    在作项目的时候遇到须要将文件转为base64编码,并存储在文件中.程序员 在将文件转为base64编码是会将文件读入内存,进行base64编码,输出到文件中.代码入下:编码 1spa 2code 3内 ...

  5. Android 内存溢出解决方案(OOM) 整理总结

    在最近做的工程中发现加载的图片太多或图片过大时经常出现OOM问题,找网上资料也提供了很多方法,但自己感觉有点乱,特此,今天在不同型号的三款安卓手机上做了测试,因为有效果也有结果,今天小马就做个详细的总 ...

  6. 【Android 内存优化】Bitmap 内存占用计算 ( Bitmap 图片内存占用分析 | Bitmap 内存占用计算 | Bitmap 不同像素密度间的转换 )

    文章目录 一.Bitmap 内存占用 二.Bitmap 内存占用计算示例 三.Bitmap 内存占用与像素密度 四.Bitmap 内存占用与像素密度示例 一.Bitmap 内存占用 在 Android ...

  7. android webview 内存溢出,Android 内存溢出和内存泄漏的问题

    Android 内存溢出和内存泄漏的问题 在面试中,经常有面试官会问"你知道什么是内存溢出?什么是内存泄漏?怎么避免?"通过这篇文章,你可以回答出来了. 内存溢出(OOM)是指程序 ...

  8. android 避内存溢出,Android避免内存溢出(Out of Memory)方法总结

    Android避免内存溢出(Out of Memory)方法总 结 避免内存溢出的方法,主要是对以下三个方面对程序进行优化武汉Android培训 内存引用 在处理内存引用之前,我们先来复习下什么是强引 ...

  9. android如何避免内存泄露,Android开发中应该避免的内存泄露

    一.背景和目的: 目前许多开发人员在Android开发过程中,较少关注实现细节和内存使用,容易会造成内存泄露,导致程序OOM. 本文会通过代码向大家介绍在Android开发过程中常见的内存泄露. 二. ...

最新文章

  1. 微博:推动世界的力量(第2版)
  2. jquery mobile app案例_讲座预告 | 运动app内的个人成就、运动轨迹和社交分享对体能提升的长期效应研究...
  3. yii mysql 2002_YII 错误 SQLSTATE[HY000] [2002] No such file or directory
  4. 多组测试数据(求和)IV
  5. 为什么一点onclick按钮就提交表单?
  6. 直播实录丨十年主导15个产品从0到1,她的经验与思考现场拆解
  7. [转载]计算机端口详解
  8. Flash应用之百宝箱
  9. 怎么做好数据可视化(文末送书)
  10. redhat 复制文件夹及子文件夹_linux如何复制文件夹和移动文件夹
  11. ElasticSearch教程——创建索引、类型、文档
  12. 页面返回 上一页 下一页
  13. 使用U盘安装Linux系统经验总结
  14. linux cred管理
  15. 应用技术大公开系列Q之十四:(润滑).石墨烯润滑油制备工艺 (*4-2)
  16. ROS-3DSLAM(十六)lvi-sam项目总结
  17. CSS - 文本文字 增加外框、阴影、边框
  18. 消费者心理学:三个趣味经济学原理
  19. 软件体系结构期末考试复习题(题中页码 与软件体系结构原理、方法与实践第2版 张友生编著 匹配)
  20. 用matlab绘制函数图形,matlab函数绘制 用matlab怎样绘制函数图形

热门文章

  1. 【新学期、新Flag】那年花开月正圆,我,正青春
  2. 兰州公交车更换IC卡机实现“手机公交卡”
  3. shorten command line
  4. java设计模式之单例模式|单例模式之饿汉模式、懒汉模式、枚举方式|最详细的6种懒汉模式详解
  5. multipass轻量级虚拟机笔记
  6. 怎么在alert里加图片_怎么挑选实木复合地板?怎么辨别实木复合地板的好坏?...
  7. 微信小程序-元素的定位相对绝对固定
  8. 7-26 最大公约数和最小公倍数
  9. selenium生成测试报告
  10. 统计学 分布篇 - Uniform Probability Distribution(均匀分布)