Android examples for Graphics:Bitmap Size
get Bitmap By Size
//package com.java2s; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; public class Main { public static Bitmap getBitmapBySize(String path, int width, int height) { BitmapFactory.Options opts = new Options(); opts.inJustDecodeBounds = true;//from w ww . j a v a 2 s. co m BitmapFactory.decodeFile(path, opts); opts.inSampleSize = computeSampleSize(opts, -1, width * height); opts.inJustDecodeBounds = false; Bitmap bm = BitmapFactory.decodeFile(path, opts); return bm; } private static int computeSampleSize(Options opts, int minSideLength, int maxNumOfPixels) { int initialSize = computeInitialSampleSize(opts, 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(Options opts, int minSideLength, int maxNumOfPixels) { double w = opts.outWidth; double h = opts.outHeight; int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(w * h / maxNumOfPixels); int upperBound = (minSideLength == -1) ? 128 : (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; } } }