Getting Bitmap from Custom SurfaceView

12,491

Your question became clear only by your last comment. In the code that you posted above, you are returning with return bitmap. That will return the local variable bitmap. This local variable is totally blank. You may be drawing to your bitmap or associating an image to it, somewhere else in the code. But the instance of bitmap in your above code, is blank and only local to the function. You cannot expect it to return your updated, latest bitmap.

Now, after your comment I googled "getting a bitmap of current surfaceview" and it led me to this SO answer: Create Bitmap from SurfaceView

In that question, it was apparently solved by extending View instead of SurfaceView. Drawing cache only works for View.

Update: Follow the below tutorials. Based on the code pasted by you, it's not clear what the error is. There are a lot of things that need be done for drawing to SurfaceView, and I'm not sure if you have done those, and I cannot ask for every such missing item. I followed below tutorials for my basic graphics projects. You need to read them and see if you have missed anything.

Tutorials on Canvas drawing:

Playing with Graphics in android all parts.

Android 2D

Share:
12,491
Lpc_dark
Author by

Lpc_dark

I am a student in school and I am hoping to make some little apps for practice and just gather as much experience as possible. Love Programming and hope to be really good one day to start giving back :D

Updated on July 26, 2022

Comments

  • Lpc_dark
    Lpc_dark almost 2 years

    I have this code in a class that extends surface view and implements runnable I am able to use the class basically allows you to draw to the canvas using different colors and such. I'm trying to get a method that will allow me to save the image after it is drawn and this is the method. No matter what i do i just get a black image with nothing on it. Any ideas?

    I have cache drawing enabled

    Objective get a bitmap image from a custom SurfaceView I have exhausted options of looking at some other post on here and didn't find anything to work. Here is hoping that there is recently a new solution to this. Many thanks

    public Bitmap getImage() {
                Bitmap bitmap = Bitmap.createBitmap(this.getWidth(),
                        this.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                this.draw(canvas);
                return bitmap;
            }
    
  • Lpc_dark
    Lpc_dark over 11 years
    There must be a way. I've seen others use it successfully. Just not the coded. I used a view before it didn't work because the performance of it wasn't as quick. What i tried to do now was to make a field Image and canvas. Then i placed the image into the canvas and when i call onDraw i just send in my canvas instead of the one they wanted to. Still didn't work... any idea what draws to the surfaceView and when it is called. So i can insert my canvas instead of the one that it uses.
  • sanjeev mk
    sanjeev mk over 11 years
    For not letting SurfaceView itself draw to the canvas, and letting your own onDraw() to do the task, call setWillNotDraw(false) from your class constructor.
  • Lpc_dark
    Lpc_dark over 11 years
    Thanks ye i already included it. Sigh still can't get the image out of it. I have a log inside onDraw and im only seeing it called when i first open the application. It's not called when i am drawing to the surface view? when is it called by anychance?
  • sanjeev mk
    sanjeev mk over 11 years
    When you call invalidate() your onDraw() is called again. What I'd suggest is, do your drawings in a new Thread. In the run() method of the Thread, you can update the Canvas and call invalidate(). This will call onDraw() and show updated canvas. run() is also called once on thread creation, so you have to implement a while(true) infinite loop inside run() to keep updating your drawing.
  • Lpc_dark
    Lpc_dark over 11 years
    I wont be able to call invalidate because only the Thread that created it will be able to access it. just tried