Android(OPhone)对图片进行缩放

在OPhone或者Android系统上如何对图片进行缩放?废话不多说,直接上代码。

    /**
     *
     * @param ctx
     * @param uri 原图片uri
     * @param bm 将图片转换后的bitmap
     * @param widthLimit
     * @param heightLimit
     * @return
     */
   public static Bitmap checkPictureSize(Context ctx, Uri uri, Bitmap bm,
                   int widthLimit, int heightLimit) {
           int outWidth = bm.getWidth();
           int outHeight = bm.getHeight();
 

           int s = 1;
           while ((outWidth / s > widthLimit) || (outHeight / s > heightLimit)) {
                   s *= 2;
           }
           Log.i(TAG, "outWidth=" + outWidth / s + " outHeight=" + outHeight / s);
           BitmapFactory.Options options = new BitmapFactory.Options();
           options.inSampleSize = s;
           options.inPreferredConfig = Bitmap.Config.ARGB_8888;
 

           InputStream input = null;
           try {
                   input = ctx.getContentResolver().openInputStream(uri);
                   return BitmapFactory.decodeStream(input, null, options);
           } catch (FileNotFoundException e) {
                   Log.e(TAG, "FileNotFoundException ", e);
                   return null;
           } finally {
                   if (input != null) {
                           try {
                                   input.close();
                           } catch (IOException e) {
                                   Log.e(TAG, "IOException ", e);
                           }
                   }
           }
   }

你可能感兴趣的:(ophone,android,input,null)