Android examples for Graphics:Bitmap Create
create Bitmap by width and height
//package com.java2s; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Main { public static Bitmap createBitmap(String path, int w, int h) { try {/*from w ww .j av a2 s. c o m*/ BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inJustDecodeBounds = true; // inJustDecodeBoundstrue BitmapFactory.decodeFile(path, opts); int srcWidth = opts.outWidth;// int srcHeight = opts.outHeight;// int destWidth = 0; int destHeight = 0; // double ratio = 0.0; if (srcWidth < w || srcHeight < h) { ratio = 0.0; destWidth = srcWidth; destHeight = srcHeight; } else if (srcWidth > srcHeight) {// maxLength ratio = (double) srcWidth / w; destWidth = w; destHeight = (int) (srcHeight / ratio); } else { ratio = (double) srcHeight / h; destHeight = h; destWidth = (int) (srcWidth / ratio); } BitmapFactory.Options newOpts = new BitmapFactory.Options(); // inSampleSizeSDK2 newOpts.inSampleSize = (int) ratio + 1; // inJustDecodeBoundsfalse newOpts.inJustDecodeBounds = false; // inSampleSize newOpts.outHeight = destHeight; newOpts.outWidth = destWidth; // return BitmapFactory.decodeFile(path, newOpts); } catch (Exception e) { return null; } } }