我正在尝试旋转相机图像。代码如下所示:
Bitmap result = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getWidth(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.save();
canvas.rotate(90);
canvas.translate(0, -1*bitmap.getHeight());
canvas.drawBitmap(bitmap, new Matrix(), null);
canvas.restore();
String path = String.format(dir+"pic_%d.jpg", System.currentTimeMillis());
stream = new FileOutputStream(path);
bitmap.compress(CompressFormat.JPEG, this.imageQuality, stream);
stream.flush();
stream.close();
问题是:我收到 OutOfMemoryException(第一行)。有没有办法通过额外的线程或其他解决方案来完成这项工作?缩小图像目前不是一种选择。
最佳答案
如果您的图像中不需要 alpha channel ,您可以使用 RGB_565 而不是 ARGB_8888。这将为您每个像素节省 2 个字节。
关于android:如何改进图像处理的内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9452723/