android图片压缩

1、根据图片宽、高压缩图片

public static Bitmap getBitmap(String filePath) {

final BitmapFactory.Options options =new BitmapFactory.Options();

options.inJustDecodeBounds=true;

BitmapFactory.decodeFile(filePath, options);

// Calculate inSampleSize

options.inSampleSize=calculateInSampleSize1(options, 480, 800);

// Decode bitmap with inSampleSize set

options.inJustDecodeBounds=false;

return BitmapFactory.decodeFile(filePath, options);

}

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;

}

2、压缩Bitmap质量

public static byte[] Bitmap2Bytes(Bitmap bm)

{

ByteArrayOutputStream baos =new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 80, baos);

return baos.toByteArray();

}

你可能感兴趣的:(android图片压缩)