1. Compression by setting the sampling rate
res resource image compression decodeResource
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { // First decode with inJustDecodeBounds=true to check dimensions final options = new (); = true; (res, resId, options); // Calculate inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); // Decode bitmap with inSampleSize set = false; return (res, resId, options); }
uri image compression decodeStream
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) { Bitmap bitmap = null; try { options = new (); = true; (getContentResolver().openInputStream(uri), null, options); = (options, UtilUnitConversion.dip2px(, reqWidth), UtilUnitConversion.dip2px(, reqHeight)); = false; bitmap = (getContentResolver().openInputStream(uri), null, options); } catch (Exception e) { (); } return bitmap; }
Local File url image compression
public static Bitmap getloadlBitmap(String load_url, int width, int height) { Bitmap bitmap = null; if (!(load_url)) { File file = new File(load_url); if (()) { FileInputStream fs = null; try { fs = new FileInputStream(file); } catch (FileNotFoundException e) { (); } if (null != fs) { try { opts = new (); = true; ((), null, opts); = false; = true; = true; = new byte[32 * 1024]; = (opts, UtilUnitConversion.dip2px(, width), UtilUnitConversion.dip2px(, height)); = false; bitmap = ((), null, opts); } catch (IOException e) { (); } finally { if (null != fs) { try { (); } catch (IOException e) { (); } } } } } } return bitmap; }
Calculate SampleSize based on the displayed image size
public int calculateInSampleSize( options, int reqWidth, int reqHeight) { if (reqWidth == 0 || reqHeight == 0) { return 1; } // Raw height and width of image final int height = ; final int width = ; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that 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; }
Call method:
(decodeSampledBitmapFromResource(getResources(), , 100, 100))
Bitmap bitmap = decodeSampledBitmapFromUri(cropFileUri);
(mContext, mImage, (url, 100, 100), .ic_login_head, true);
2. Quality compression: The specified image is reduced to below xkb
// Compress to below 100kb int maxSize = 100 * 1024; public static Bitmap getBitmapByte(Bitmap oriBitmap, int maxSize) { ByteArrayOutputStream out = new ByteArrayOutputStream(); (, 100, out); byte[] fileBytes = (); int be = (maxSize * 100) / ; if (be > 100) { be = 100; } (); (, be, out); return oriBitmap; }
3. A way to avoid Oom simply by obtaining the width and height of the picture
This class has a field called inJustDecodeBounds. The description of this member in the SDK is as follows:
If set to true, the decoder will return null (no bitmap), but the out...
In other words, if we set it to true, then (String path, Options opt) will not really return a Bitmap to you. It will only retrieve its width and height back to you, so that it will not occupy too much memory and will not occur so frequently.
/** * Get Options according to res to get width and height outWidth and * @param res * @param resId * @return */ public static decodeOptionsFromResource(Resources res, int resId) { // First decode with inJustDecodeBounds=true to check dimensions final options = new (); = true; (res, resId, options); return options; }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.