1

I have a MappedByteBuffer with a portion of it containing the bytes of a PNG file.

I want to call: BitmapFactory.decodeByteArray(byte[] data, int offset, int length);

Do I have to move the portion of the MappedByteBuffer into a byte[] or is there some way to just pass the MappedByteBuffer?

Thanks!

Fra
  • 393
  • 7
  • 19

1 Answers1

1

There is no way to call a method that does not exist.

The only way to use data from MappedByteBuffer is to copy the data into a byte[] buffer, then passing this buffer to the method you mentioned. Probably via ByteBuffer#get(byte[] target).

zapl
  • 63,179
  • 10
  • 123
  • 154
  • Ya it's what I figured. Just wanted to make sure there wasn't some other faster/less resource intensive wat to move PNG bytes into a Bitmap object. – Fra Oct 28 '14 at 20:43
  • There are possibly better approaches, this one requires a full in-memory copy of the image data. MappedByteBuffer is from a file, so the filename, a filedescriptor or a stream would be more efficient. For example when the image data is just part of a file, you could create an `InputStream` that covers just those parts - advantage: data is not requried to be fully in memory. – zapl Oct 28 '14 at 20:48
  • Ya I was thinking that. Might experiment. For now though I was stick with this approach as I find the MappedByteBuffers to be a lot quicker. And no matter what means I use of parsing the data I am stuck creating a byte[] equal to the image size... – Fra Oct 28 '14 at 20:54
  • hmm, if you have a mapped buffer anyways, this might actually be the fastest approach since the data is already in memory / if not gets there really fast because mapped buffers should profit from being "free" of kernel<>user context switcheridoo ( http://www.ibm.com/developerworks/library/j-zerocopy/ ). – zapl Oct 28 '14 at 21:09