zoukankan      html  css  js  c++  java
  • 017_01setImageBitmap引起的OOM分析

      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 选取较大的那个缩放比例来设置。

    源代码如下:

     1 package com.example.day17_01pictureloader;
     2 
     3  import android.app.Activity;
     4 import android.graphics.Bitmap;
     5 import android.graphics.BitmapFactory;
     6 import android.graphics.BitmapFactory.Options;
     7 import android.os.Bundle;
     8 import android.os.Debug;
     9 import android.os.Environment;
    10 import android.view.Display;
    11 import android.view.Menu;
    12 import android.view.MenuItem;
    13 import android.view.View;
    14 import android.widget.ImageView;
    15 
    16 public class MainActivity extends Activity {
    17 
    18     @Override
    19     protected void onCreate(Bundle savedInstanceState) {
    20         super.onCreate(savedInstanceState);
    21         setContentView(R.layout.activity_main);
    22     }
    23     
    24     public void loadpicture(View v){
    25         ImageView  iv_iamge = (ImageView) findViewById(R.id.iv_image);
    26         //iv_iamge.setImageResource(R.drawable.fun1);
    27         String path = Environment.getExternalStorageDirectory().getAbsolutePath()
    28                 +"/DCIM/100ANDRO/cameras/dd.jpg";
    29         Bitmap bm = BitmapFactory.decodeFile(path);    
    30         iv_iamge.setImageBitmap(bm);    
    31     }
    32         
    33     public void scaleloadpicture(View v){
    34         String path = Environment.getExternalStorageDirectory().getAbsolutePath()
    35                 +"/DCIM/100ANDRO/cameras/pp.jpeg";
    36         //decode需要添加一些option
    37         Options opt =new Options();
    38         opt.inJustDecodeBounds = true;
    39         Bitmap bm = BitmapFactory.decodeFile(path,opt);    
    40         int original_width = opt.outWidth;
    41         int original_heigth= opt.outHeight  ;
    42         System.out.println("图片的原始宽高"+original_width+","+original_heigth);
    43         
    44         //获取屏幕的宽高
    45         Display display = getWindowManager().getDefaultDisplay();
    46         int screenWidth = display.getWidth();
    47         int screenHeight = display.getHeight();
    48         System.out.println("屏幕的宽和高: " + screenWidth + " * " + screenHeight);
    49         
    50         //计算缩放比例
    51         int scale_width =  original_width/screenWidth ;
    52         int scale_height =  original_heigth/screenHeight ;
    53         int scale = scale_width>scale_height? scale_scale_height;
    54         System.out.println("缩放的比例: " + scale  );
    55 
    56         //重新设置decode的参数,加上缩放比例的参数
    57         opt.inJustDecodeBounds = false;
    58        // opt.inScaled = scale;
    59         opt.inSampleSize =scale;
    60         bm = BitmapFactory.decodeFile(path,opt);
    61         
    62         //将缩放后的图片加载到imageview上
    63         ImageView  iv_iamge = (ImageView) findViewById(R.id.iv_image);
    64         iv_iamge.setImageBitmap(bm);
    65     }
    66 }
     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context="com.example.day17_01pictureloader.MainActivity" 
    10     android:orientation="vertical">
    11 
    12     <TextView
    13         android:layout_width="wrap_content"
    14         android:layout_height="wrap_content"
    15         android:text="@string/hello_world" />
    16         <Button
    17         android:layout_width="wrap_content"
    18         android:layout_height="wrap_content"
    19         android:text="加载图片" 
    20         android:onClick="loadpicture"/>
    21            <Button
    22         android:layout_width="wrap_content"
    23         android:layout_height="wrap_content"
    24         android:text="缩放加载图片" 
    25         android:onClick="scaleloadpicture"/>
    26     <ImageView
    27         android:id="@+id/iv_image"
    28         android:layout_width="wrap_content"
    29         android:layout_height="wrap_content"
    30          />
    31 </LinearLayout>

    物随心转,境由心造,一切烦恼皆由心生。
  • 相关阅读:
    nginx模块学习——nginx_http_push_module模块深入讲解和聊天室实现
    常见的qq在线客服代码
    MongoDB数据库介绍及安装(一)
    Python 创建类
    Python backup脚本
    Python 类的初始化小案例
    Python 类实例化
    Python 类初始化__init__
    ObjC(ObjectiveC): NSString应该用initWithFormat? 还是 stringWithFormat?
    NSUserDefaults
  • 原文地址:https://www.cnblogs.com/woodrow2015/p/4541143.html
Copyright © 2011-2022 走看看