android - 缩放后保存全尺寸图像的位图

标签 android image bitmap out-of-memory surfaceview

我的应用程序使用 SurfaceView 向用户显示图像,并让他们在已拍摄的图像上操作贴纸。为了减少内存使用,我缩放了所有这些位图以适合屏幕。现在我想保存用户刚刚贴上所有贴纸的图像,我想用原始图像的分辨率保存它。如果不将全尺寸图像加载到内存中并冒 OutOfMemoryError 的风险,如何才能做到这一点?我不知道从哪里开始,使用给定的工具这似乎是一项不可能完成的任务。

最佳答案

如果您想保存位图而不缩小它的尺寸。有两种选择。

  1. 在您的应用程序标记( list )中使用 largeHeap=true。
  2. 如果您能承受一点质量损失!使用压缩位图。

示例代码

ByteArrayOutputStream out = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, out);
    Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

下面是 Bitmap 类的压缩方法文档的链接。 http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

关于android - 缩放后保存全尺寸图像的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33085895/

相关文章:

android - 在 SDK 管理器中看不到 android 15 和 13

android - Android Activity/服务中的多个线程

android - 如何从我的 Android 应用程序发送电子邮件?

java - 如何将图像放入 GoogleMaps api fragment 中

JavaScript 事件不适用于 getElementById

c# - 剪辑区域不起作用

C# 在另一个位图中搜索一个位图

android - 如何克隆一个 Parcel 对象

node.js - 下载图像并使用 multipart/form-data 将其发送到 API

c - 从 2 个位图中获取 on 位索引的组合