public static byte[] getByteArray(final String filePath, int maxLen) {
InputStream inStream = new FileInputStream(new File(filePath));
Bitmap bmp = BitmapFactory.decodeStream(inStream);
ByteArrayOutputStream output = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
UtilsLog.d(TAG, "bmpToByteArray a len:" + output.toByteArray().length);
int options = 100;
while (output.toByteArray().length > maxkb && options != 10) {
output.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG, options, output);
UtilsLog.d(TAG, "bmpToByteArray b options:" +options+" len:"+ output.toByteArray().length);
options -= 10;
}
UtilsLog.d(TAG, "bmpToByteArray c len:" + output.toByteArray().length);
return output.toByteArray();
}
decodeStream對(duì)于大圖片非常耗時(shí),
最終壓縮長度為最初的0.029 即不到3%