您的位置:首页 > 其它

017_01setImageBitmap引起的OOM分析

2015-05-30 22:10 281 查看
  OOM的全称是out of memory exception.当使用setImageBitmap()显示图片时有时会引起该异常。那么是什么原因呢?我们知道一张图片是有很多个像素点组成的,bmp格式图片一个像素需要3字节(RGB,每个颜色一个字节表示)或者4字节(RGBA)表示。如果一张图片分辨率为4266*2900 ,那么它有 12371400 像素点,使用setImageBitmap()所需内存为:12371400 *4byte=49485616字节约48M,当前模拟器设置的VM heap是32M,所以会报OOM异常。

  该如何解决呢?假设模拟器的分辨率我320*480。在分辨率有限的情况下,可以采取缩放图片的方法。4266/320 =13 2900/480 =6 选取较大的那个缩放比例来设置。

源代码如下:

package com.example.day17_01pictureloader;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.os.Bundle;
import android.os.Debug;
import android.os.Environment;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void loadpicture(View v){
ImageView  iv_iamge = (ImageView) findViewById(R.id.iv_image);
//iv_iamge.setImageResource(R.drawable.fun1);
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+"/DCIM/100ANDRO/cameras/dd.jpg";
Bitmap bm = BitmapFactory.decodeFile(path);
iv_iamge.setImageBitmap(bm);
}

public void scaleloadpicture(View v){
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+"/DCIM/100ANDRO/cameras/pp.jpeg";
//decode需要添加一些option
Options opt =new Options();
opt.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeFile(path,opt);
int original_width = opt.outWidth;
int original_heigth= opt.outHeight  ;
System.out.println("图片的原始宽高"+original_width+","+original_heigth);

//获取屏幕的宽高
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
System.out.println("屏幕的宽和高: " + screenWidth + " * " + screenHeight);

//计算缩放比例
int scale_width =  original_width/screenWidth ;
int scale_height =  original_heigth/screenHeight ;
int scale = scale_width>scale_height? scale_width:scale_height;
System.out.println("缩放的比例: " + scale  );

//重新设置decode的参数,加上缩放比例的参数
opt.inJustDecodeBounds = false;
// opt.inScaled = scale;
opt.inSampleSize =scale;
bm = BitmapFactory.decodeFile(path,opt);

//将缩放后的图片加载到imageview上
ImageView  iv_iamge = (ImageView) findViewById(R.id.iv_image);
iv_iamge.setImageBitmap(bm);
}
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.day17_01pictureloader.MainActivity"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载图片"
android:onClick="loadpicture"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="缩放加载图片"
android:onClick="scaleloadpicture"/>
<ImageView
android:id="@+id/iv_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: