Skip to content

Commit 04091b0

Browse files
committed
解决MIUI系统照片可以崩溃的Bug
1 parent 7028828 commit 04091b0

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

.idea/vcs.xml

-6
This file was deleted.

app/src/main/java/com/nanchen/compressimage/MainActivity.java

+14
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ private void initInstances() {
4848
}
4949

5050
public void compress(View view) {
51+
// 默认的压缩方法,多张图片只需要直接加入循环即可
5152
newFile = CompressHelper.getDefault(this).compressToFile(oldFile);
53+
54+
// 你也可以自定义压缩
55+
// newFile = new CompressHelper.Builder(this)
56+
// .setMaxWidth(720) // 默认最大宽度为720
57+
// .setMaxHeight(960) // 默认最大高度为960
58+
// .setQuality(80) // 默认压缩质量为80
59+
// .setCompressFormat(CompressFormat.JPEG) // 设置默认压缩为jpg格式
60+
// .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
61+
// Environment.DIRECTORY_PICTURES).getAbsolutePath())
62+
// .build()
63+
// .compressToFile(oldFile);
64+
65+
5266
mImageNew.setImageBitmap(BitmapFactory.decodeFile(newFile.getAbsolutePath()));
5367
mTextNew.setText(String.format("Size : %s", getReadableFileSize(newFile.length())));
5468
}

compresshelper/src/main/java/com/nanchen/compresshelper/ImageUtil.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,24 @@ static Bitmap getScaledBitmap(Context context, Uri imageUri, float maxWidth, flo
5858
int actualHeight = options.outHeight;
5959
int actualWidth = options.outWidth;
6060

61+
if (actualHeight == -1 || actualWidth == -1){
62+
try {
63+
ExifInterface exifInterface = new ExifInterface(filePath);
64+
actualHeight = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, ExifInterface.ORIENTATION_NORMAL);//获取图片的高度
65+
actualWidth = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, ExifInterface.ORIENTATION_NORMAL);//获取图片的宽度
66+
} catch (IOException e) {
67+
e.printStackTrace();
68+
}
69+
}
70+
6171
if (actualWidth < 0 || actualHeight < 0) {
6272
Bitmap bitmap2 = BitmapFactory.decodeFile(filePath);
63-
actualWidth = bitmap2.getWidth();
64-
actualHeight = bitmap2.getHeight();
73+
if (bitmap2 != null){
74+
actualWidth = bitmap2.getWidth();
75+
actualHeight = bitmap2.getHeight();
76+
}else{
77+
return null;
78+
}
6579
}
6680

6781
float imgRatio = (float) actualWidth / actualHeight;

0 commit comments

Comments
 (0)