package com.fanxin.app.comments; import java.io.File; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class PictureUtil { /** * 计算图片的缩放值 * * @param options * @param reqWidth * @param reqHeight * @return */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio = Math.round((float) height / (float) reqHeight); final int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 根据路径获得图片并压缩返回bitmap * * @param filePath * @return */ public static Bitmap getSmallBitmap(String filePath) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(filePath, options); options.inSampleSize = calculateInSampleSize(options, 480, 800); options.inJustDecodeBounds = false; return BitmapFactory.decodeFile(filePath, options); } /** * 获取保存图片的目录 * * @return */ @SuppressLint("SdCardPath") public static File getAlbumDir() { File dir = new File("/mnt/sdcard/bizchat/"); if (!dir.exists()) { dir.mkdirs(); } return dir; } }