图片二次采样有效防止因图片引起的OOM
来源:互联网 发布:淘宝怎么样投诉店家 编辑:程序博客网 时间:2024/04/28 07:30
OOM也就是我们经常遇到的:OutOfMenoryError 内存溢出异常
int image_width = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
int image_lenfth = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
//得到屏幕的宽和高
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
int screen_width = manager.getDefaultDisplay().getWidth();
int screen_height = manager.getDefaultDisplay().getHeight();
//通过图片的宽和高以及屏幕的宽和高得到一个压缩比的值
int width = image_width/screen_width;
int height = image_lenfth/screen_height;
int score = 0;
if(width>=height&&width>1){
score=width;
}else if(height>=width&&height>1){
score=height;
}
//返回Bitmap对象
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=score;
Bitmap bitmap = BitmapFactory.decodeFile("图片路径",options);
出现的原因那就是当加载的东西超出内存范围或内存不够用时会出现
解决方案:
ExifInterface anInterface = new ExifInterface("图片路径")
int image_width = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, 0);
int image_lenfth = anInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, 0);
//得到屏幕的宽和高
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
int screen_width = manager.getDefaultDisplay().getWidth();
int screen_height = manager.getDefaultDisplay().getHeight();
//通过图片的宽和高以及屏幕的宽和高得到一个压缩比的值
int width = image_width/screen_width;
int height = image_lenfth/screen_height;
int score = 0;
if(width>=height&&width>1){
score=width;
}else if(height>=width&&height>1){
score=height;
}
//返回Bitmap对象
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=score;
Bitmap bitmap = BitmapFactory.decodeFile("图片路径",options);
阅读全文
1 0
- 图片二次采样有效防止因图片引起的OOM
- 图片的二次采样,防止图片加载导致内存不足
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- 图片的二次采样
- Ribbon通过代码自定义负载均衡规则
- plsql字体修改
- HTML5之常用行级标签及用法
- 在windows中开启Hyper-V与android studio模拟器冲突问题
- 卫星结构分析的若干点
- 图片二次采样有效防止因图片引起的OOM
- hdu 1800 (哈希)
- 有关cocos2d创建c++项目,并把win32项目打包成apk文件
- 层次聚类
- BLE-NRF51822教程4-串口BLE解析
- React Navigation之Tab Navigator完全解析
- TCP并发服务器程序,每个客户一个子进程
- 好用的post请求方法
- FFMPEG视音频编解码零基础学习方法【摘自雷博士文章】