## ByteBuffer.array() does not have a binding

_Submitted by a community member on 2014-06-20 18:40 UTC_

As several people on the forums have pointed out, ByteBuffer.array() does not have a binding (and improperly using Object.ToArray\<byte\>() doesn't work either). E.g. <http://forums.xamarin.com/discussion/5950/how-to-convert-from-bitmap-to-byte-without-bitmap-compress> I provided the code in that forum post for calling JNI directly to avoid the crazy workarounds others have posted (such as writing it out to a file and reading it back in), but it's a problem that it's missing from the default Xamarin bindings.

Xamarin.Android   4.12.5.2 (419335316003113183185da56b888fffe0875ae2)

Thanks! :)

* * *

_Xamarin Bugzilla comment 1 by a community member on 2014-06-23 07:30 UTC_

I have check this issue and I am able to reproduce this issue.

Steps to reproduce this issue:

1\. Create an android application on XS.
2\. Write the following line of code on "OnCreate()" method of "MainActivity.cs" class
```
       
     Bitmap bitmap = BitmapFactory.DecodeResource(Resources,Resource.Drawable.Icon);
     ByteBuffer byteBuffer = ByteBuffer.Allocate(bitmap.ByteCount);
     bitmap.CopyPixelsToBuffer(byteBuffer);
     byte[] bytes = byteBuffer.ToArray<byte>();
```

3\. Debug the application.
4\. Exception will be occur.

I observed that when I use byteBuffer.ToArray\<byte\>() it doesn't work and it throws an exception i.e "Unhandled Exception:
[mono] System.InvalidCastException: Unable to cast from 'java/nio/ByteArrayBuffer' to '[B'."

Screencast: <https://gist.github.com/anonymous/e1ab40b0f42060734b67>

Build Output: <http://www.screencast.com/t/ju8rnisv>

Environment Info:

=== Xamarin Studio ===

Version 5.0.1 (build 3)
Installation UUID: 851620d2-e950-4682-b459-991ccec9d895
Runtime:
```
 Microsoft .NET 4.0.30319.18408
 GTK+ 2.24.22 (MS-Windows theme)
 GTK# 2.12.25
```

=== Xamarin.Android ===

Version: 4.12.4 (Business Edition)
Android SDK: D:\\SDK\\AndroidSDK
```
 Supported Android versions:
  2.1   (API level 7)
  2.2   (API level 8)
  2.3   (API level 10)
  3.1   (API level 12)
  3.2   (API level 13)
  4.0   (API level 14)
  4.0.3 (API level 15)
  4.1   (API level 16)
  4.2   (API level 17)
  4.3   (API level 18)
  4.4   (API level 19)
```

Java SDK: C:\\Program Files (x86)\\Java\\jdk1.6.0\_31
java version "1.6.0\_31"
Java(TM) SE Runtime Environment (build 1.6.0\_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

=== Build Information ===

Release ID: 500010003
Git revision: f94ee866936d25105704eb63728ad5a981eda0a4
Build date: 2014-06-04 12:22:49-04
Xamarin addins: 1a6044e8321ea07e03a56b5381951686c82fed8b

=== Operating System ===

Windows 6.1.7601.65536 (64-bit)

* * *

_Xamarin Bugzilla comment 2 by Atsushi Eno [MSFT] on 2014-06-24 13:46 UTC_

(This cannot be fixed until we bring new API generator that brings API breakage -\> XA 5.0 - or later - milestone.)

* * *

_Xamarin Bugzilla comment 3 by a community member on 2014-08-13 10:14 UTC_

It looks like this might be related to issues I'm having with the Wrap methods in IntBuffer and ByteBuffer:

Steps to reproduce this issue:

1\. Create an android application on XS.
2\. Write the following line of code on "OnCreate()" method of "MainActivity.cs"
class:

```
  sbyte[] byteArray = new sbyte[1] { 0 };
  ByteBuffer buffer = ByteBuffer.Wrap(byteArray);
```

```
  buffer.Position(0);
```

```
  sbyte before = buffer.Get(0);
  byteArray[0] = 1;
  sbyte after = buffer.Get(0);
```

3\. Debug the application.
4\. Observe the 'after' variable - it is 0, like the 'before' variable. I expected the buffer to have changed since the wrapped byteArray changed.

Equivalent code in Java works as expected, see <http://ideone.com/xo4oqW>

Environment info:

=== Xamarin Studio ===

Version 5.2.1 (build 1)
Installation UUID: f8d409f3-5086-458c-bbbb-86ee9d31ee43
Runtime:
```
	Microsoft .NET 4.0.30319.18444
	GTK+ 2.24.22 (MS-Windows theme)
	GTK# 2.12.25
```

=== Xamarin.Android ===

Version: 4.14.0 (Trial Edition)
Android SDK: K:\\adt-bundle-windows-x86\_64-20131030\\sdk
```
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.4   (API level 19)
```

Java SDK: C:\\Program Files (x86)\\Java\\jdk1.6.0\_39
java version "1.6.0\_39"
Java(TM) SE Runtime Environment (build 1.6.0\_39-b04)
Java HotSpot(TM) Client VM (build 20.14-b01, mixed mode, sharing)

=== Build Information ===

Release ID: 502010001
Git revision: d06832ce9807d6be24aca225457e8b37c7669f6f
Build date: 2014-08-07 14:35:09-04
Xamarin addins: 1de032531be4cecf2f39dbee3b87aac78204058c

=== Operating System ===

Windows 6.1.7601.65536 (64-bit)

* * *

_Xamarin Bugzilla comment 4 by Atsushi Eno [MSFT] on 2014-08-19 06:54 UTC_

Reassigning this to dean, it's totally up to the new jar parser.

* * *

_Xamarin Bugzilla comment 5 by a community member on 2017-04-16 16:15 UTC_

what's the status of this bug.
it's not fix even for the latest mono.android framework!

* * *

_Xamarin Bugzilla comment 6 by a community member on 2017-04-16 16:17 UTC_

It's very important for nio base application which port from android platform!

* * *

_Xamarin Bugzilla comment 7 by a community member on 2017-08-23 19:06 UTC_

any news on this?

* * *

_Xamarin Bugzilla comment 8 by a community member on 2018-03-09 22:28 UTC_

Oh my god! My Android app is inches away from being finished but this bug is a show stopper! I can't get it over the line if I can't convert a ByteBuffer to an array of bytes. Is there really no way to do this currently? I tried this code, but I get a timeout:

```
            Marshal.Copy(byteBuffer.GetDirectBufferAddress(), buffers, 0, 64);
```


* * *

<br />

_Reference: <https://bugzilla.xamarin.com/show_bug.cgi?id=20772>_