Android加载一张3.4MB的图片,不出现OOM
来源:互联网 发布:淘宝店可以转天猫吗 编辑:程序博客网 时间:2024/05/04 00:36
package com.jianda.zuci.showbigphoto;import android.content.res.Resources;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView iv_bigPhoto; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_bigPhoto = (ImageView)findViewById(R.id.iv_bigPhoto); int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);//得到系统内存大小 Log.v("maxMemory",maxMemory+"KB"); iv_bigPhoto.setImageBitmap(decodeSampledBitmapFromResource(getResources(),R.drawable.bigphoto,256,144)); } public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth,int reqHeight){ final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1;//If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. if (height > reqHeight || width > reqWidth){ final int heightRatio = Math.round((float)height / (float)reqHeight); final int widthRatio = Math.round((float)width / (float)reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; Log.v("inSampleSize",Integer.toString(inSampleSize)); } return inSampleSize; } public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true;//属性设置为true就可以让解析方法禁止为bitmap分配内存,返回值也不再是一个Bitmap对象,而是null BitmapFactory.decodeResource(res,resId,options); options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight); Log.v("压缩比",Integer.toString(options.inSampleSize)); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options); }}
运行效果:
0 0
- Android加载一张3.4MB的图片,不出现OOM
- android加载图片出现OOM
- listView加载几百张图片不出现OOM的方法
- Android开发中 页面加载一张超大图片(561kb)时出现OOM
- Android开发中 页面加载一张超大图片(561kb)时出现OOM
- Android开发中 页面加载一张超大图片(561kb)时出现OOM
- Android开发中 页面加载一张超大图片 时出现OOM
- Android开发中 页面加载一张超大图片(561kb)时出现OOM
- Android 加载打图片出现OOM异常的处理方式
- Android加载图片时出现OOM(OutOfMermary)的问题解决方案
- Android 解决加载图片过多出现oom--强大的Fresco
- ANDROID图片加载时出现OOM的一般处理方法
- Android如何加载大图不出现OOM
- android中加载图片时出现oom
- Android避免加载图片出现OOM
- 加载图片出现OOM
- 解决加载图片出现OOM的方法
- android显示一张大的网络图片,避免OOM
- iOS提取本地通讯录数据
- 深入理解 Python
- google guava 测试
- Android手机 Fildder真机抓包
- java 类加载器和类加载方式
- Android加载一张3.4MB的图片,不出现OOM
- 一步步学会Thrift服务搭建和调用
- Andfix热修复框架原理及源码解析-下篇
- hdu1026(bfs求起点到终点最短距离)
- 《水芙蓉》
- Linux的“孤儿进程”的“父进程”
- 关于Makefile的几个博文
- LeetCode 327 Count of Range Sum(分治)
- 简单计算机