Skip to content

Commit

Permalink
解决MIUI系统照片可以崩溃的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
nanchen2251 committed Mar 18, 2017
1 parent 7028828 commit 04091b0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
6 changes: 0 additions & 6 deletions .idea/vcs.xml

This file was deleted.

14 changes: 14 additions & 0 deletions app/src/main/java/com/nanchen/compressimage/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,21 @@ private void initInstances() {
}

public void compress(View view) {
// 默认的压缩方法,多张图片只需要直接加入循环即可
newFile = CompressHelper.getDefault(this).compressToFile(oldFile);

// 你也可以自定义压缩
// newFile = new CompressHelper.Builder(this)
// .setMaxWidth(720) // 默认最大宽度为720
// .setMaxHeight(960) // 默认最大高度为960
// .setQuality(80) // 默认压缩质量为80
// .setCompressFormat(CompressFormat.JPEG) // 设置默认压缩为jpg格式
// .setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
// Environment.DIRECTORY_PICTURES).getAbsolutePath())
// .build()
// .compressToFile(oldFile);


mImageNew.setImageBitmap(BitmapFactory.decodeFile(newFile.getAbsolutePath()));
mTextNew.setText(String.format("Size : %s", getReadableFileSize(newFile.length())));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,24 @@ static Bitmap getScaledBitmap(Context context, Uri imageUri, float maxWidth, flo
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;

if (actualHeight == -1 || actualWidth == -1){
try {
ExifInterface exifInterface = new ExifInterface(filePath);
actualHeight = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_LENGTH, ExifInterface.ORIENTATION_NORMAL);//获取图片的高度
actualWidth = exifInterface.getAttributeInt(ExifInterface.TAG_IMAGE_WIDTH, ExifInterface.ORIENTATION_NORMAL);//获取图片的宽度
} catch (IOException e) {
e.printStackTrace();
}
}

if (actualWidth < 0 || actualHeight < 0) {
Bitmap bitmap2 = BitmapFactory.decodeFile(filePath);
actualWidth = bitmap2.getWidth();
actualHeight = bitmap2.getHeight();
if (bitmap2 != null){
actualWidth = bitmap2.getWidth();
actualHeight = bitmap2.getHeight();
}else{
return null;
}
}

float imgRatio = (float) actualWidth / actualHeight;
Expand Down

0 comments on commit 04091b0

Please sign in to comment.