转载自http://blog.csdn.net/junjx/article/details/7798604

在Android开发中,我们经常需要加载图片。但是图片的尺寸往往会很大,如果我们要的是比较小的图片,在Android有限的内存下,我们显然不能把大尺寸的图片放到内存里,这样不但效率降低,而且会导致java.lang.OutOfMemory异常,相信很多朋友都遇到过这样的问题,那么该怎么解决这一问题呢?其实在Android官方文档中早已给出了解决的方案。

一、读Bitmap的尺寸和类型

BitmapFactory类提供一系列的方法 (decodeByteArray(),decodeFile(),decodeResource(), etc.)从资源中创建一个Bitmap。可以在这些方法中选择适当的方法对图片资源进行译码。然而这些方法会很容易导致OutOfMemoryError异常。每种类型的解码方法都有一些附加的属性,你可以基于BitmapFactory.Options类提供的方法定义指定的解码方式。

设置inJustDecodeBounds属性为true,避免分配内存,返回一个null的Bitmap对象(包含outWidth,outHeight andoutMimeType),这样就可以读取图片的尺寸和类型了。


BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeResource(getResources(), R.id.myimage, options);int imageHeight = options.outHeight;int imageWidth = options.outWidth;String imageType = options.outMimeType;

二、加载一个缩小版到内存

现在我们已经获取了图片的大小,接下来我们该考虑是加载原图到内存,还是加载一张缩略图到内存。这有几点需要考虑的:

估算加载图片占用内存的大小

你打算在你的程序中给分出多少内存来加载图片

你加载到目标组件图片的尺寸大小如何

当前设备的屏幕的尺寸和密度大小

例如,我们要把一张1024x768像素的图片加载到一个128x96像素的ImageView里面,我们应该告诉解码员(decoder)加载一张子图到内存,这样我们就需要设置BitmapFactory.Options对象的inSampleSize属性,例如一张2048x1536分辨率的图片,如果设置inSampleSize=4,将会产生一张分辨率为512x384的图片,加载这张子图到内存只需要消耗0.75MB的内存,而加载原图却需要12MB的内存(假设bitmap配置是ARGB_8888)。以下是代码是根据图片的大小及子图的尺寸动态计算
imSampleSize的值的方法。

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) {        if (width > height) {            inSampleSize = Math.round((float)height / (float)reqHeight);        } else {            inSampleSize = Math.round((float)width / (float)reqWidth);        }    }    return inSampleSize;}

接着,首先设置inJustDecodeBounds=true,通过options设置inSampleSize的值再设置inJustDecodeBounds=false:

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

这样我们就能方便的得到一张分辨率为100x100的子图了:

mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

你可以用类似的方法,根据你的需要采用其他的译码方法来处理图片。

更多相关文章

  1. Android(安卓)matrix 控制图片的旋转、缩放、移动
  2. android拍照与读取相册
  3. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  4. Android(安卓)P SystemUI之StatusBar UI布局status_bar.xml解析
  5. Android--SoLoader,android动态加载so库
  6. Android异步加载图像小结 (含线程池,缓存方法)
  7. android解决坚屏拍照和保存图片旋转90度的问题,并兼容4.0
  8. Android(安卓)Paging组件Demo
  9. Android(安卓)TabHost使用、动态加载内容

随机推荐

  1. Android:Task '' not found in root proj
  2. android 底部菜单写法之FragmentTabHost
  3. android webview 设置
  4. AndroidStudio打包apk出现的问题 `Error:
  5. react-native开发实例之替换默认logo——
  6. Android(安卓)Studio如何引用外部Library
  7. Android(安卓)JNI和NDK学习(3)--Android.
  8. AndroidStudio用gradle编译中文乱码
  9. 播放音乐ProgressBar进度条
  10. 举例说明android中ProgressBar的用法