/**
* 保存图片到指定路径
* Save image with specified size
*
* @param filePath the image file save path 储存路径
* @param bitmap the image what be save 目标图片
* @param size the file size of image 期望大小
*/
private static File saveImage(String filePath, Bitmap bitmap, long size) {
File result = new File(filePath.substring(0, filePath.lastIndexOf("/")));
if (!result.exists() && !result.mkdirs()) return null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
int options = 100;
bitmap.compress(CompressFormat.JPEG, options, stream);
while (stream.toByteArray().length / 1024 > size && options > 6) {
stream.reset();
options -= 6;
bitmap.compress(CompressFormat.JPEG, options, stream);
}
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(stream.toByteArray());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
return new File(filePath);
}