Bug 20772 - ByteBuffer.array() does not have a binding
Summary: ByteBuffer.array() does not have a binding
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: Bindings (show other bugs)
Version: 4.12.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: dean.ellis
URL:
Depends on:
Blocks:
 
Reported: 2014-06-20 18:40 UTC by David Schwegler
Modified: 2018-03-09 22:28 UTC (History)
8 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 20772 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description David Schwegler 2014-06-20 18:40:59 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! :)
Comment 1 Ram Chandra 2014-06-23 07:30:06 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: http://www.screencast.com/t/ju8rnisv

Build Output: https://gist.github.com/anonymous/e1ab40b0f42060734b67

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)
Comment 2 Atsushi Eno 2014-06-24 13:46:51 UTC
(This cannot be fixed until we bring new API generator that brings API breakage -> XA 5.0 - or later - milestone.)
Comment 3 Ola Østtveit 2014-08-13 10:14:02 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)
Comment 4 Atsushi Eno 2014-08-19 06:54:25 UTC
Reassigning this to dean, it's totally up to the new jar parser.
Comment 5 congzhangzh 2017-04-16 16:15:06 UTC
what's the status of this bug.
it's not fix even for the latest mono.android framework!
Comment 6 congzhangzh 2017-04-16 16:17:14 UTC
It's very important for nio base application which port from android platform!
Comment 7 Seifer 2017-08-23 19:06:47 UTC
any news on this?
Comment 8 Melbourne Developer 2018-03-09 22:28:17 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);