再次优化图片压缩方式, 提问和反馈(先压缩再显示界面)

This commit is contained in:
kehaoyuan
2018-01-16 14:18:36 +08:00
parent c0e7b8e3ee
commit 8c3335a7bf
8 changed files with 52 additions and 44 deletions

View File

@ -28,13 +28,16 @@ public class BitmapUtils {
*/
public static boolean savePicture(String newPath, String filePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
File file = new File(newPath);
int quality = 80;
do {
try {
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos);
bos.flush();
@ -60,13 +63,18 @@ public class BitmapUtils {
* @return
*/
public static boolean savePicture(String newPath, String filePath, int compressSize) {
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
if (compressSize > new File(filePath).length() && bitmap.getWidth() < 960 && bitmap.getHeight() < 960) {
if (compressSize > new File(filePath).length()) {
return false;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos);
float zoom = (float) Math.sqrt(compressSize / (float) bos.toByteArray().length);