android 彩信 图片大小,Android_Message里面彩信图片的压缩方法

//最大宽度 :widthLimit 最大高度:heightLimit 最大size:byteLimit

private byte[] getResizedImageData(int widthLimit, int heightLimit, int byteLimit) {

int outWidth = mWidth;//图片本身宽度

int outHeight = mHeight;//图片本身高度

float scaleFactor = 1.F;//压缩比例

while ((outWidth * scaleFactor > widthLimit) || (outHeight * scaleFactor > heightLimit)) {

scaleFactor *= .75F;//一直改变压缩比例,直到确定压缩后的宽度和高度比限制小为止

}

InputStream input = null;

try {

ByteArrayOutputStream os = null;

int attempts = 1;

int sampleSize = 1;

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

int quality = MessageUtils.IMAGE_COMPRESSION_QUALITY;

Bitmap b = null;

// In this loop, attempt to decode the stream with the best possible subsampling (we

// start with 1, which means no subsampling - get the original content) without running

// out of memory.

do {

input = mContext.getContentResolver().openInputStream(mUri);

options.inSampleSize = sampleSize;

try {

b = BitmapFactory.decodeStream(input, null, options);

} catch (OutOfMemoryError e) {

Log.w(TAG, "getResizedImageData: img too large to decode (OutOfMemoryError), " +

"may try with larger sampleSize. Curr sampleSize=" + sampleSize);

sampleSize *= 2; // works best as a power of two

attempts++;

continue;

} finally {

if (input != null) {

try {

input.close();

} catch (IOException e) {

Log.e(TAG, e.getMessage(), e);

}

}

}

} while (b == null && attempts < NUMBER_OF_RESIZE_ATTEMPTS);

if (b == null) {

return null;

}

attempts = 1; // reset count for second loop

// In this loop, we attempt to compress/resize the content to fit the given dimension

// and file-size limits.

do {

try {

if (options.outWidth > widthLimit || options.outHeight > heightLimit ||

(os != null && os.size() > byteLimit)) {

// The decoder does not support the inSampleSize option.

// Scale the bitmap using Bitmap library.

int scaledWidth = (int)(outWidth * scaleFactor);

int scaledHeight = (int)(outHeight * scaleFactor);

if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {

Log.v(TAG, "getResizedImageData: retry scaling using " +

"Bitmap.createScaledBitmap: w=" + scaledWidth +

", h=" + scaledHeight);

}

b = Bitmap.createScaledBitmap(b, scaledWidth, scaledHeight, false);

if (b == null) {

return null;

}

}

// Compress the image into a JPG. Start with MessageUtils.IMAGE_COMPRESSION_QUALITY.

// In case that the image byte size is still too large reduce the quality in

// proportion to the desired byte size.

os = new ByteArrayOutputStream();

b.compress(CompressFormat.JPEG, quality, os);

int jpgFileSize = os.size();

if (jpgFileSize > byteLimit) {//如果压缩后的大小比限制大,就改变quality再进行压缩(或者压缩次数达到,不能再压缩)

quality = (quality * byteLimit) / jpgFileSize; // watch for int division!

if (quality < MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY) {

quality = MessageUtils.MINIMUM_IMAGE_COMPRESSION_QUALITY;//quality有最小值限制

}

os = new ByteArrayOutputStream();

b.compress(CompressFormat.JPEG, quality, os);

}

} catch (java.lang.OutOfMemoryError e) {}

scaleFactor *= .75F;

attempts++;

} while ((os == null || os.size() > byteLimit) && attempts < NUMBER_OF_RESIZE_ATTEMPTS);

b.recycle(); // done with the bitmap, release the memory

return os == null ? null : os.toByteArray();

} catch (FileNotFoundException e) {

Log.e(TAG, e.getMessage(), e);

return null;

} catch (java.lang.OutOfMemoryError e) {

Log.e(TAG, e.getMessage(), e);

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值