Android:压缩位图的有效方法

发布于 2025-02-11 10:50:17 字数 835 浏览 1 评论 0原文

是否有一种体面的方法可以压缩位图而不重新缩放它而不会吃太多的记忆会导致OOM?

Bitmap compressedImageFile;
try {
    compressedImageFile = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
    
    //Converting bitmap to array stream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    //Decreasing the quality for thumbnail
    compressedImageFile.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
    
    //Convert Bitmap to Byte array
    byte[] thumbData = byteArrayOutputStream.toByteArray();

    ...

bytearrayoutputstream.tobytearray()正在引起OOM,可以通过减少一半的位图大小来避免这种情况,但可能有一个更体面的方法?

这是我正在使用的地方

Is there a decent way to compress a Bitmap without re-scaling it without eating too much memory that cause OOM?

Bitmap compressedImageFile;
try {
    compressedImageFile = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
    
    //Converting bitmap to array stream
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    //Decreasing the quality for thumbnail
    compressedImageFile.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
    
    //Convert Bitmap to Byte array
    byte[] thumbData = byteArrayOutputStream.toByteArray();

    ...

byteArrayOutputStream.toByteArray() is causing OOM, this can be avoid by reducing the bitmap size in a half but probably there is a more decent approach?

This is where I am using it
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文