In my Android app, I have a Bitmap object and an int[]. One algorithm in my app copies the Bitmap pixel values to the int[] and then modifies the values in the latter.
I want to save the contents of the raw int[] to disk as quickly as possible. What is a fast and memory efficient way to do this? (i.e. calling a .writeInt function on each int[] item would be a very slow way to do this in Android as there is no JIT on most devices)
I found that if I create a byte[] of the same size as the int[] and copy the int[] to the byte[], I can then use one of many file io methods to write the byte[] to disk in one API call. However, this uses a lot of extra memory as I now have two versions of the int[].
Failing this, perhaps there is some method that will directly write the int values of a Bitmap into a file? If so, I could copy the int[] back into the Bitmap object and use this.
By the way, I don’t want to use “.png” compression and just want to save the raw values as the latter is much quicker.
I think you will have to allocate a buffer (
byte[]), of a size that maximizes performance and still uses only a reasonable amount of memory. Maybe something like 8K.You then loop over your array in pages ( buffer size / 4 ), fill the buffer and write it out. The next page can then reuse the same buffer. Make sure you correctly handle the last page, which may be half empty (of course with a bitmap, it probably aligns to 8K buffers anyway).