我继承了这段代码,并试图弄清楚如何让它更有效,这样我就不必抛出 OutOfMemory 异常。这适用于 writeBitmap 和 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) {
}
}