Taking a screenshot

Monkey Targets Forums/Android/Taking a screenshot

thoast(Posted 2014) [#1]
Tried every solution from google in every combination.. dont know any further.

This one crashes:
public static Bitmap loadBitmapFromView(View v) {
		Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
		Canvas c = new Canvas(b);
		v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
		v.draw(c);
		return b;
	}
with v = BBAndroidGame.AndroidGame().GetGameView()

Many others, like this one, only show black image when saved:

BBAndroidGame.GameView v = BBAndroidGame.AndroidGame().GetGameView();
		
		v.setDrawingCacheEnabled(true);

		// this is the important code :)  
		// Without it the view will have a dimension of 0,0 and the bitmap will be null          
		v.measure(v.getWidth(), v.getHeight());
		v.layout(0, 0, v.getWidth(), v.getHeight());

		v.buildDrawingCache(true);
		Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
		v.setDrawingCacheEnabled(false); // clear drawing cache
		
		return b;	


Can anybody help me? =/


Gerry Quinn(Posted 2014) [#2]
Run it in the emulator and press PrintScreen ;)


Danilo(Posted 2014) [#3]
Function Screenshot:Image()
    Local width:Int  = mojo.app.DeviceWidth()
    Local height:Int = mojo.app.DeviceHeight()
    Local img:Image  = mojo.graphics.CreateImage(width,height)
    If img
        Local mem:Int[] = New Int[width*height]
        If mem
            mojo.graphics.ReadPixels(mem,0,0,width,height)
            img.WritePixels(mem,0,0,width,height)
            Return img
        Endif
    Endif
    Return Null
End

Function SaveScreenshot:Bool(filename:String)
    ' Import brl.databuffer
    ' Import brl.filestream
    '
    ' example: SaveScreenshot("monkey://internal/screenshot_("+String(DeviceWidth())+"x"+String(DeviceHeight())+").raw")

    Local width:Int  = mojo.app.DeviceWidth()
    Local height:Int = mojo.app.DeviceHeight()
    Local mem:Int[] = New Int[width*height]
    If mem
        mojo.graphics.ReadPixels(mem,0,0,width,height)
        Local buffer:DataBuffer = New DataBuffer(width*height*4)
        If buffer
            buffer.PokeInts(0,mem,0,width*height)
            Local stream:FileStream = FileStream.Open(filename,"w")
            If stream
                stream.WriteAll(buffer,0,buffer.Length())
                buffer.Discard()
                Return True
            Endif
        Endif
    Endif
    Return False
End



skid(Posted 2014) [#4]
With device connected to computer, find and run in the tools folder of your android_sdk the monitor app, select your android device, then click the Screen Capture camera icon.


erebel55(Posted 2014) [#5]
Wow this is awesome :) I didn't know about that, thanks skid!


EdzUp(Posted 2014) [#6]
Press volume down and power together to take a snapshot off the screen on device


thoast(Posted 2014) [#7]
@Danilo: Thank you. In this case i would have to write things like jpeg compression myself, right?


thoast(Posted 2014) [#8]
I got it working this way, important is that you call the function in the render loop.

Android / JAVA:
public static Bitmap getBitmap(int x, int y, int w, int h) {	
		gxtkGraphics device = bb_graphics.g_renderDevice;
		
		int width = w;
		int height = w;
		
		int[] pixels = new int[width*height];
		device.ReadPixels(pixels, x, y, width, height, 0, width);		
		
		Bitmap bitmap=Bitmap.createBitmap( width,height,Bitmap.Config.ARGB_8888 );
		
		device.Flush();
		bitmap.setPixels( pixels,0,width,0,0,width, height );
		
		return bitmap;
	}