首页 > 解决方案 > 如何压缩图像?

问题描述

我正在 android studio 中进行一个项目,我正在上传图像。一些图像的尺寸非常大,我想在将它们上传到应用程序之前对其进行压缩。谁能给我一些建议?

标签: android

解决方案


试试这个功能

 public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, boolean filter) {

    float ratio = Math.min(
            maxImageSize / realImage.getWidth(),
            maxImageSize / realImage.getHeight());
    int width = Math.round(ratio * realImage.getWidth());
    int height = Math.round(ratio * realImage.getHeight());

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
            height, filter);
    return newBitmap;
}

推荐阅读