3

I am creating a bitmap from glsurfaceview and adding it to a arraylist but when I created a bitmap from glsurfaceview it gives outofmemory error

CODE:

Bitmap bitmap = createBitmapFromGLSurface(0, 0, mEffectView.getWidth(),
            mEffectView.getHeight(), gl);
al_bitmaps.add(bitmap);

Method:

private Bitmap createBitmapFromGLSurface(int x, int y, int w, int h, GL10 gl)
        throws OutOfMemoryError {
    int bitmapBuffer[] = new int[w * h];
    int bitmapSource[] = new int[w * h];
    IntBuffer intBuffer = IntBuffer.wrap(bitmapBuffer);
    intBuffer.position(0);

    try {
        gl.glReadPixels(x, y, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE,
                intBuffer);
        int offset1, offset2;
        for (int i = 0; i < h; i++) {
            offset1 = i * w;
            offset2 = (h - i - 1) * w;
            for (int j = 0; j < w; j++) {
                int texturePixel = bitmapBuffer[offset1 + j];
                int blue = (texturePixel >> 16) & 0xff;
                int red = (texturePixel << 16) & 0x00ff0000;
                int pixel = (texturePixel & 0xff00ff00) | red | blue;
                bitmapSource[offset2 + j] = pixel;
            }
        }
    } catch (GLException e) {
        return null;
    }

    return Bitmap.createBitmap(bitmapSource, w, h, Bitmap.Config.ARGB_8888);
}

For larger resolution device (ex. Samsung Galaxy S4), my app crashes.

I would like to know how to set inSampleSize of that bitmap.

genpfault
  • 51,148
  • 11
  • 85
  • 139
Manish Agrawal
  • 551
  • 2
  • 6
  • 22

1 Answers1

4

First of all i will recommended to you use

android:largeHeap="true"

in your manifest file.

Secondary if that not helped to you look that https://stackoverflow.com/a/17839597/2956344

Also i recommend to you know about maximum texture size of your GLSufaceView to check limit of your bitmap resolution programmatically.

Get Maximum OpenGL ES 2.0 Texture Size Limit in Android

P.S. I think you find information about use GL10.

Community
  • 1
  • 1
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119