android - 如何压缩图像?
问题描述
我正在 android studio 中进行一个项目,我正在上传图像。一些图像的尺寸非常大,我想在将它们上传到应用程序之前对其进行压缩。谁能给我一些建议?
解决方案
试试这个功能
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;
}
推荐阅读
- c - 如何使用 mbedtls_pk_verify 验证签名
- ansible - 使用 Ansible Tower 针对 Openstack 运行 playbook
- javascript - 将文件拖放到特定目标
- c++ - std::string 构造函数如何处理固定大小的 char[]?
- c# - 如何使用 C# 本地化在 Selenium WebDriver 中不断更改其 ID 中的数字的元素?
- javascript - 将数据从原生 android 应用程序发送到 Ionic 本地索引文件
- javascript - 来自 JSON 对象列表的 Charts.js 中的堆积条形图
- ffmpeg - Discord.NET 1.0.2 将语音发送到语音通道不起作用
- azure - 如何为 Azure CLI 使用 Powershell splatting
- amazon-web-services - AWS S3 跨区域复制删除操作行为的状态