title: Taking screenshot of Android OpenGL
body: <p>I'm trying to take a screenshot of Android OpenGL.</p>
  
  <p>The code I found is as follows:</p>
  
  <pre><code>nt size = width * height;
      ByteBuffer buf = ByteBuffer.allocateDirect(size * 4);
      buf.order(ByteOrder.nativeOrder());
      glContext.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, buf);
      int data[] = new int[size];
      buf.asIntBuffer().get(data);
      buf = null;
      Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
      bitmap.setPixels(data, size-width, -width, 0, 0, width, height);
      data = null;
  
      short sdata[] = new short[size];
      ShortBuffer sbuf = ShortBuffer.wrap(sdata);
      bitmap.copyPixelsToBuffer(sbuf);
      for (int i = 0; i &lt; size; ++i) {
          //BGR-565 to RGB-565
          short v = sdata[i];
          sdata[i] = (short) (((v&amp;0x1f) &lt;&lt; 11) | (v&amp;0x7e0) | ((v&amp;0xf800) &gt;&gt; 11));
      }
      sbuf.rewind();
      bitmap.copyPixelsFromBuffer(sbuf);
  
      try {
          FileOutputStream fos = new FileOutputStream("/sdcard/screeshot.png");
          bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
          fos.flush();
          fos.close();
      } catch (Exception e) {
          // handle
      }
  </code></pre>
  
  <p>I tried also a code from that site
  <a href="http://www.anddev.org/how_to_get_opengl_screenshot__useful_programing_hint-t829.html" rel="noreferrer">link text</a></p>
  
  <p>In each case the result is a png file which is completely black.
  I found there is some problem with <code>glReadPixels</code> method but I don't know how to bypass it.</p>