title: JNI: Bitmap from unsigned char* always null
body: <p>I would like to pass an image (via jni) from C++ to an android application. I am starting from an <code>unsigned char*</code> array. This array is not corrupted whatsoever; I am even able to save it into a <code>ppm</code> file and display it properly on my laptop.</p>
  
  <p>Then, I convert it to a <code>jByteArray</code> by using this function:</p>
  
  <pre><code>jbyteArray imgByte=as_byte_array(env,imgRaw,img.getRawImageSize());
  ...
  jbyteArray as_byte_array(JNIEnv *env, unsigned char* buf, int len) {
      jbyteArray array = env-&gt;NewByteArray (len);
      env-&gt;SetByteArrayRegion (array, 0, len, reinterpret_cast&lt;jbyte*&gt;(buf));
      return array;
  }
  </code></pre>
  
  <p>Afterward, I send this <code>jByteArray</code> to the java side. This variable has been properly populated, as I can see by printing its hex values on LogCat:</p>
  
  <pre><code>07-01 18:02:45.941    7017-8238/com.myapp.myapp W/C++ side﹕  798a95798b9677889371838d6b7d8664757e5d6e7860717b5e...
  07-01 18:02:46.941    7017-8238/com.myapp.myapp W/Java side﹕ 798a95798b9677889371838d6b7d8664757e5d6e7860717b5e...
  </code></pre>
  
  <p>The final step would be to show it on an ImageView. To this end, I do the following code (taken from another question on SO):</p>
  
  <pre><code>public void setImageViewWithByteArray(final ImageView view, byte[] data) {
      final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
  
      getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
              if (bitmap==null) {
                  Log.e(TAG,"Bitmap is NULL!");
                  view.setImageResource(R.drawable.abc_btn_radio_material);
              }
              else {
                  view.setImageBitmap(bitmap);
              }
          }
      });
  }
  </code></pre>
  
  <p>but the <code>bitmap</code> variable is always <code>null</code>. What am I doing wrong? Is there a way to debug <code>decodeByteArray</code>?</p>