I am new to Android development. I am investigating ways to render OpenGL off-screen to a bitmap (I want to dynamically render icons for use in my app), and I saw jsemler's post about using the EGL buffers to accomplish this (and I already sent him a PM), but I'm curious why my approach doesn't work.
I know that you can use View.getDrawingCache() to grab a bitmap of a view or control, but when I try the same on a GLSurfaceView, I get a NullPointerException.
Why does this work:
- Code: Select all
Button b = new Button(context);
b.setText("Hello World");
b.setDrawingCacheEnabled(true);
b.layout(0, 0, 100, 100);
b.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(b.getDrawingCache());
b.setDrawingCacheEnabled(false);
myImageView.setImageBitmap(b);
And this doesn't:
- Code: Select all
GLSurfaceView newView = new GLSurfaceView(this);
newView.setRenderer(new MyRenderer());
newView.setDrawingCacheEnabled(true);
newView.layout(0, 0, 100, 100);
newView.buildDrawingCache(); // This line crashes
Bitmap b = Bitmap.createBitmap(newView.getDrawingCache());
myImageView.setImageBitmap(b);
Here is the stack trace from the exception:
java.lang.NullPointerException
at android.view.SurfaceView.updateWindow(SurfaceView.java:282)
at android.view.SurfaceView.dispatchDraw(SurfaceView.java:264)
at android.view.View.buildDrawingCache(View.java:6040)
at android.view.View.buildDrawingCache(View.java:5894)
at com.test.android.LoginActivity$1.onClick(LoginActivity.java:70)
at android.view.View.performClick(View.java:2344)
at android.view.View.onTouchEvent(View.java:4133)
at android.widget.TextView.onTouchEvent(TextView.java:6510)
at android.view.View.dispatchTouchEvent(View.java:3672)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
(This stack trace is from when I was calling the code from a button click, but I also get a crash when I move the code into my activity's onCreate.)
Maybe I'm just trying to do something crazy, but this seems like an extremely convenient way to render 3D to an off-screen texture if I can get this working.
Thanks,
Sha Sha


