图片压缩优化 以及 问题详情页面调整
This commit is contained in:
@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user