I have a large int array containing image data in the format ARGB (alpha, R, G and B channels, 1 byte each). I want to save it to file in onPause() to be able to reload it when the app is restarted. What do you think is the best way to do that?
I found the following methods:
-
Convert the int array to a byte array manually (see here) and then use a
FileOutputStreamto output the byte array. -
Wrap the array into a
java.nio.IntBufferand then write the object to file usingjava.io.ObjectOutputStream.writeObject(). -
Write each element one at a time using
java.io.ObjectOutputStream.writeInt().
All these methods seem quite wasteful so there is probably another, better way. Possibly even a way to use image compression to reduce the size of the file?
None of those. Some of them don’t even make sense.
then call
dos.writeInt()as many times as necessary, then closedos. The buffer will take away most of the pain.Or else create an
IntBufferand useFileChannel.write(), but I’ve never been able to figure out how that works in the absence of anIntBuffer.asByteBuffer()method. Or else create aByteBuffer, take it as anIntBufferviaasIntBuffer(), put the data in, then adjust theByteBuffer‘s length, which is another missing piece of the API, and again useFileChannel.write(ByteBuffer).