引用库implementation 'id.zelory:compressor:2.1.0'
转成Bitmap
try {
bitmap = new Compressor(activity)
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.compressToBitmap(file);
} catch (IOException e) {
e.printStackTrace();
}
转成File
try {
file2 = new Compressor(getContext())
.setMaxWidth(640)
.setMaxHeight(480)
.setQuality(75)
.setCompressFormat(Bitmap.CompressFormat.WEBP)
.setDestinationDirectoryPath(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath())
.compressToFile(new File(picPath));
} catch (IOException e) {
e.printStackTrace();
}
|