title: Android bitmap image to Unity C# texture
body: <p>I'm in the draw loop of an android view:</p>
  
  <pre><code>        Bitmap bitmap = Bitmap.createBitmap(this.getWidth(), 
          this.getHeight(), Bitmap.Config.ARGB_4444);
          Canvas newCanvas = new Canvas(bitmap);
          super.draw(newCanvas);
          Log.d("AndroidUnity","Canvas Drawn!");
          mImageView.setImageBitmap(bitmap);
  </code></pre>
  
  <p>And the above code shows me the <strong>correct</strong> drawing on the attached Image Viewer.</p>
  
  <p>When I convert the bitmap to a byte array:</p>
  
  <pre><code>        ByteBuffer byteBuffer = ByteBuffer.allocate(bitmap.getByteCount());
          bitmap.copyPixelsToBuffer(byteBuffer);
          byte[] bytes = byteBuffer.array();
  </code></pre>
  
  <p>importing the bytes into Unity does not work (shows a black image on my rawimage):</p>
  
  <pre><code>        imageTexture2D = new Texture2D(width, height, TextureFormat.ARGB4444, false);
          imageTexture2D.LoadRawTextureData(bytes);
          imageTexture2D.Apply();
          RawImage.texture = imageTexture2D;
  </code></pre>
  
  <p>Any ideas on how to get the Java bytes[] to display as a texture/image in Unity? I've tested that the bytes are sending correctly, i.e. when I push a byte array of {1,2,3,4} from android, I get {1,2,3,4} on the unity side.</p>
  
  <p>this isn't mentioning that Unity throws an error when trying to transfer the bytes as a byte[], so instead I have to follow <a href="https://forum.unity.com/threads/androidjavaproxy-lang-reflect-array-exception.257348/" rel="nofollow noreferrer">this</a> advice, on the C# side:</p>
  
  <pre><code>void ReceieveAndroidBytes(AndroidJavaObject jo){
  AndroidJavaObject bufferObject = jo.Get&lt;AndroidJavaObject&gt;("Buffer");
  byte[] bytes = AndroidJNIHelper.ConvertFromJNIArray&lt;byte[]&gt;(bufferObject.GetRawObject()); }
  </code></pre>
  
  <p>and a trivial byte[] container class "Buffer" on the java side</p>