From 18bc47efe13c186aa906343ee0bcadef28d3f512 Mon Sep 17 00:00:00 2001 From: kehaoyuan Date: Mon, 15 Jan 2018 11:53:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=8E=8B=E7=BC=A9=E4=BC=98?= =?UTF-8?q?=E5=8C=96=20=E4=BB=A5=E5=8F=8A=20=E9=97=AE=E9=A2=98=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E9=A1=B5=E9=9D=A2=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/gh/common/util/BitmapUtils.java | 18 +++++++++++------- .../main/res/layout/questionsdetail_item.xml | 4 ++-- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/gh/common/util/BitmapUtils.java b/app/src/main/java/com/gh/common/util/BitmapUtils.java index 5a1c6ff4f3..500c87e196 100644 --- a/app/src/main/java/com/gh/common/util/BitmapUtils.java +++ b/app/src/main/java/com/gh/common/util/BitmapUtils.java @@ -11,7 +11,6 @@ import android.media.ExifInterface; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; @@ -60,11 +59,13 @@ public class BitmapUtils { * @param filePath * @return */ - - // TODO 应该根据图片分辨率大小 采取不同的压缩方式 (质量/分辨率) 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) { + return false; + } ByteArrayOutputStream bos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 85, bos); @@ -72,12 +73,11 @@ public class BitmapUtils { Matrix matrix = new Matrix(); matrix.setScale(zoom, zoom); - Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bos.reset(); + bitmap.recycle(); result.compress(Bitmap.CompressFormat.JPEG, 85, bos); - while (bos.toByteArray().length > compressSize) { matrix.setScale(0.9f, 0.9f); @@ -86,12 +86,16 @@ public class BitmapUtils { result.compress(Bitmap.CompressFormat.JPEG, 85, bos); } - File file = new File(newPath); try { BufferedOutputStream fbos = new BufferedOutputStream(new FileOutputStream(file)); result.compress(Bitmap.CompressFormat.JPEG, 85, fbos); - } catch (FileNotFoundException e) { + fbos.flush(); + fbos.close(); + + result.recycle(); + } catch (Exception e) { + file.delete(); e.printStackTrace(); return false; } diff --git a/app/src/main/res/layout/questionsdetail_item.xml b/app/src/main/res/layout/questionsdetail_item.xml index 67f5519463..e8301e2623 100644 --- a/app/src/main/res/layout/questionsdetail_item.xml +++ b/app/src/main/res/layout/questionsdetail_item.xml @@ -88,7 +88,7 @@ android:id = "@+id/questionsdetail_item_answercount" android:layout_width = "wrap_content" android:layout_height = "wrap_content" - android:layout_marginTop = "10dp" + android:layout_marginTop = "9dp" android:textColor = "@color/hint" android:textSize = "12sp" app:layout_constraintTop_toBottomOf = "@id/questionsdetail_item_picll" /> @@ -112,7 +112,7 @@ android:layout_marginTop = "9dp" android:maxEms = "8" android:textColor = "@color/hint" - android:textSize = "11sp" + android:textSize = "12sp" android:maxLines="1" app:layout_constraintRight_toRightOf = "parent" app:layout_constraintTop_toBottomOf = "@id/questionsdetail_item_picll" />