How to solve the oom problem of Android parsing picture!!! _android

Source: Internet
Author: User

Everyone good, today to share is to resolve the problem of the resolution of the picture Oom, we can use bitmapfactory here a variety of decode methods, if the picture is very small, will not appear oom, but when the picture is very large

It is necessary to use bitmapfactory.options this dongdong, options in the main two parameters are more important.

Options.injustdecodebounds = false/true; 
Picture compression ratio. 

We go to parse a picture, if too large, it will oom, we can set the compression ratio insamplesize, but this compression ratio setting is a problem, so we parse the picture can be divided into two steps, the first step is

Get the width of the picture, here to set the Options.injustdecodebounds=true, this time the decode bitmap is null, just put the picture of the wide height in the options, Then the second step is to set the appropriate compression ratio insamplesize, this time to obtain the appropriate bitmap. Here I draw a simple flowchart, as follows:

In order to make it easier for everyone to understand, I made a simple demo here, the main function is an interface has a imageview, click ImageView, enter the local album, select a picture, ImageView control display selected pictures. The demo steps are as follows:

The first step is to create a new Android project named Imagecachedemo. The directory structure is as follows:

The second step is to create a new Imagecacheutil.java tool class with the following code:

Package com.tutor.oom; 
 
Import Java.io.InputStream; 
Import Android.content.ContentResolver; 
Import Android.content.Context; 
Import Android.graphics.Bitmap; 
Import Android.graphics.BitmapFactory; 
Import android.graphics.BitmapFactory.Options; 
 
Import Android.net.Uri; 
 /** * @author Frankiewei. 
 * Tool class. 
   * * public class Imagecacheutil {/** * get the right bitmap usually get bitmap use this method. 
   * @param path. 
   * @param data byte[] array. 
   * @param context * @param URI URI * @param the target template wide or high size. * @param width * @return/public static Bitmap Getresizedbitmap (String path, byte[] data, Contex 
 
    T Context,uri Uri, int target, Boolean width) {options options = null; 
      if (Target > 0) {Options info = new options (); 
      When set to True, decode the bitmap return is empty,//The picture is read in wide and high reading in the options. 
       
      Info.injustdecodebounds = false; 
       
      Decode (path, data, Context,uri, info); 
  int dim = Info.outwidth;    if (!width) Dim = Math.max (Dim, Info.outheight); 
 
      int ssize = SampleSize (Dim, target); 
      options = new options (); 
 
    Options.insamplesize = ssize; 
    Bitmap BM = NULL; 
    try {BM = decode (path, data, Context,uri, options); 
    catch (Exception e) {e.printstacktrace (); 
 
  return BM; 
   /** * resolves bitmap common methods. * @param path * @param data * @param context * @param URI * @param options * @return/Public Static Bitmap decode (String path, byte[] data, context, Uri Uri, bitmapfactory.options Options) {Bi 
 
    TMap result = null; 
 
    if (path!= null) {result = Bitmapfactory.decodefile (path, options); 
 
    else if (data!= null) {result = Bitmapfactory.decodebytearray (data, 0, data.length, options); 
      If else if (URI!= null) {//uri is not empty, the context is not empty. 
    Contentresolver CR = Context.getcontentresolver ();  InputStream inputstream = null; 
        try {inputstream = Cr.openinputstream (URI); 
        result = Bitmapfactory.decodestream (InputStream, NULL, options); 
      Inputstream.close (); 
      catch (Exception e) {e.printstacktrace (); 
  } return result; 
   /** * Get the right samplesize. 
   * The simple implementation here is a multiple of 2.       
      * @param width * @param target * @return/private static int samplesize (int width, int target) {      
      int result = 1; 
        for (int i = 0; i < i++) {if (Width < target * 2) {break; 
        } width = WIDTH/2;         
      result = result * 2; 
    return result; 
 } 
}

Step Three: Modify the Imagecachedemoactivity.java code as follows:

Package com.tutor.oom; 
Import android.app.Activity; 
Import android.content.Intent; 
Import Android.graphics.Bitmap; 
Import Android.os.Bundle; 
Import Android.provider.MediaStore; 
Import Android.view.View; 
Import Android.view.View.OnClickListener; 
 
Import Android.widget.ImageView; 
 /** * @author Frankiewei. 
 * Solve the picture of ordinary Oom demo. 
   * * Public class Imagecachedemoactivity extends activity {/** * Displays the ImageView of the picture. 
   
  * * Private ImageView Mimageview; 
   /** * Opens the Requestcode of the local album. 
   
  * * public static final int open_photo_requestcode = 0x1; 
   Target size of the/** * image. 
   
   
  * private static final int target = 400; 
    @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
     
    Setcontentview (R.layout.main); 
  Setupviews (); 
    private void Setupviews () {Mimageview = (ImageView) Findviewbyid (R.id.imageview); 
       
Mimageview.setonclicklistener (New Onclicklistener () {      public void OnClick (View v) {openphotos (); 
  } 
    }); 
   /** * Open a local album. 
    * * private void Openphotos () {Intent Intent = new Intent (Intent.action_pick, NULL); 
 
    Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); 
 
  Startactivityforresult (Intent, Open_photo_requestcode); @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (Request Code) {case Open_photo_requestcode:if (ResultCode = = RESULT_OK) {//If you use this method, the options are null, which is the default decode 
        will appear Oom Oh. 
         
        Bitmap BM = Imagecacheutil.decode (NULL, NULL,//Imagecachedemoactivity.this, data.getdata (), NULL); 
        This method is not oom. Just use this method.  Bitmap BM = IMAGECACHEUTIL.GETRESIZEDBITMAP (null, NULL, Imagecachedemoactivity.this, Data.getdata (), Target, 
        FALSE); 
      Mimageview.setimagebitmap (BM); 
       
   }   Break 
    Default:break; 
  } super.onactivityresult (Requestcode, ResultCode, data); 
 } 
}

Where the Main.xml layout code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= 
"http://schemas.android.com/apk/" Res/android " 
  android:layout_width=" fill_parent " 
  android:layout_height=" fill_parent " 
  android:o" rientation= "vertical" > 
 
  <textview 
    android:layout_width= "Fill_parent" 
    Wrap_content " 
    android:text=" @string/hello "/> 
   
  <imageview  
    android:id=" @+id/imageview " Android:layout_width= "400px" 
    android:layout_height= "400px" 
    android:src= "@drawable/ic_launcher" 
    /> 
 
</LinearLayout> 

The fourth step to run the above works, the effect is as follows:

Select the display from the local album. Using the Getrsizedbitmap () method, the picture is very large will not oom.
Using the default Decode method will oom.

OK, today is about here, the above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

Original link: http://blog.csdn.net/android_tutor/article/details/8099918

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.