http://developer.android.com/training/displaying-bitmaps/load-bitmap.html an official article is as follows to compress the image size after loading by resource
Method One: Load by resource
Mimageview.setimagebitmap (Decodesampledbitmapfromresource (Getresources (), R.id.myimage, 100, 100));
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);
}
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) {
Final int halfheight = HEIGHT/2;
Final int halfwidth = WIDTH/2;
Calculate the largest insamplesize value is a power of 2 and keeps both
Height and width larger than the requested height and width.
while ((halfheight/insamplesize) > Reqheight
&& (halfwidth/insamplesize) > Reqwidth) {
Insamplesize *= 2;
}
}
return insamplesize;
}
If it is not through resource, but through the Drawable object, such as getting all the app icons installed on the phone, use this method to compress the image
Method Two: Load by drawable Object
Get the compressed icon
Private drawable Getappicon (packagemanager pm, applicationinfo applicationinfo) {
try {
Oom error, use the following method
drawable drawable = Pm.getapplicationicon (ApplicationInfo);
Bitmap BM = Commonutils.drawabletobitmap (drawable);
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
byte[] data = Baos.tobytearray ();
Before compression
int Size1=bm.getbytecount ();
Bitmapfactory.options opts = new Bitmapfactory.options ();
Opts.injustdecodebounds = true;
Bitmapfactory.decodebytearray (data, 0, Data.length, opts);
Opts.insamplesize = Computesamplesize (opts,-1, 100 * 100);
Be sure to set it back to false, because we set it to true before
Opts.injustdecodebounds = false;
Bitmap bmnew =bitmapfactory.decodebytearray (data, 0, Data.length, opts);
After compression, the test of 4194304Byte compression into 16384Byte compression effect is obvious
int Size2=bmnew.getbytecount ();
return new bitmapdrawable (bmnew);
End
}catch (Exception e)
{
LOG.E ("DataCenter.getAppIcon.Exception", "Exception", e);
}
catch (OutOfMemoryError e) {
Todo:handle exception
LOG.E ("DataCenter.getAppIcon.OutOfMemoryError", "OutOfMemoryError", e);
The picture is too big to change a default small icon to the user
return new Bitmapdrawable (Bitmapfactory.decoderesource (Mcontext.getresources (), R.drawable.ic_launcher_default));
}
return null;
}
public static int Computesamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {
int initialsize = computeinitialsamplesize (options, Minsidelength, maxnumofpixels);
int roundedsize;
if (initialsize <= 8) {
Roundedsize = 1;
while (Roundedsize < initialsize) {
Roundedsize <<= 1;
}
} else {
Roundedsize = (initialsize + 7)/8 * 8;
}
return roundedsize;
}
private static int computeinitialsamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) {
Double w = options.outwidth;
Double h = options.outheight;
int lowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (math.sqrt (w * h/maxnumofpixels));
int upperbound = (Minsidelength = =-1)? : (int) math.min (Math.floor (w/minsidelength), Math.floor (h/minsidelength));
if (Upperbound < lowerbound) {
return lowerbound;
}
if ((Maxnumofpixels = =-1) && (minsidelength = =-1)) {
return 1;
} else if (minsidelength = =-1) {
return lowerbound;
} else {
return upperbound;
}
}
public static Bitmap Drawabletobitmap (drawable drawable) {
Bitmap Bitmap = Bitmap.createbitmap (Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), Drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new canvas (bitmap);
Canvas.setbitmap (bitmap);
Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());
Drawable.draw (canvas);
return bitmap;
}
Bitmapfactory offers a variety of decode methods that you can use as needed
Android Development Step by step 50:android about loading big picture Java.lang.OutOfMemoryError Error Resolution