Reputation: 31
I inherited this code and am trying to figure out how to make it more efficent so I do not have to throw an OutOfMemory exception. This is both for writeBitmap and readBitmap.
/**
* Reads bitmap from the state file
*/
void readBitmap(Bitmap bitmap) throws OutOfMemoryException {
byte[] buffer;
try {
buffer = new byte[file.length()];
} catch (OutOfMemoryError e) {
throw new OutOfMemoryException(e);
}
try {
file.readBytes(buffer, 0, 0, buffer.length);
} catch (IOException e) {
throw new RuntimeException(e);
}
Buffer byteBuffer = ByteBuffer.wrap(buffer);
try {
bitmap.copyPixelsFromBuffer(byteBuffer);
} catch (Exception e) {
}
}
Upvotes: 0
Views: 1182
Reputation: 11073
You can save out a compressed bit map. It would make it so you don't need to have an intermediate byte buffer. Just adjust the compression format to what you would like. You can also play with the size of the created memory file.
public void writeBitmap(Bitmap bitmap, int id) throws OutOfMemoryException {
final int bitmapSize = bitmap.getRowBytes() * bitmap.getHeight();
try {
file = new MemoryFile("file" + id, bitmapSize);
FileOutputStream out = file.getOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (IOException e) {
throw new RuntimeException(e);
}
size.x = bitmap.getWidth();
size.y = bitmap.getHeight();
}
Upvotes: 1