Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Simple way to capture screen and save to file for Android — Gideros Forum

Simple way to capture screen and save to file for Android

RickyngkRickyngk Member
edited August 2012 in Code snippets
After googling, I found a simple way to capture screen on Android (I forgot where the original code come from)

Use following code MyActivity.java (after export)
+ MyActivity.sRequireCapture, MyActivity.sRequireCaptureWidth, MyActivity.sRequireCaptureHeight, MyActivity.sRequireCaptureFilename : static var from MyActivity (defined by yourself)
+ TOSTRING(ANDROID_PACKAGE_NAME): replace this by your package name, such as: "com.guava7.demo"
public void onDrawFrame(GL10 gl)
{
	GiderosApplication.getInstance().onDrawFrame();
	JavaNativeBridge.onDrawFrame();
 
	if(MyActivity.sRequireCapture)
	{                    
		int width = MyActivity.sRequireCaptureWidth;
		int height = MyActivity.sRequireCaptureHeight;
		int screenshotSize = width * height;
		ByteBuffer bb = ByteBuffer.allocateDirect(screenshotSize * 4);
		bb.order(ByteOrder.nativeOrder());
		gl.glReadPixels(0, 0, width, height, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, bb);
		int pixelsBuffer[] = new int[screenshotSize];
		bb.asIntBuffer().get(pixelsBuffer);
		bb = null;
		Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
		bitmap.setPixels(pixelsBuffer, screenshotSize-width, -width, 0, 0, width, height);
		pixelsBuffer = null;
 
		short sBuffer[] = new short[screenshotSize];
		ShortBuffer sb = ShortBuffer.wrap(sBuffer);
		bitmap.copyPixelsToBuffer(sb);
 
		//Making created bitmap (from OpenGL points) compatible with Android bitmap
		for (int i = 0; i < screenshotSize; ++i) {                  
			short v = sBuffer[i];
			sBuffer[i] = (short) (((v&0x1f) << 11) | (v&0x7e0) | ((v&0xf800) >> 11));
		}
		sb.rewind();
		bitmap.copyPixelsFromBuffer(sb);
		sLastScreenShoot = bitmap;
 
		//save to sd card
		String extStorageDirectory = Environment.getExternalStorageDirectory().toString() + "//Android//data//" + TOSTRING(ANDROID_PACKAGE_NAME) + "//";
 
		File dir = new File(extStorageDirectory);
		if (!(dir.exists() && dir.isDirectory())) 
		{
			dir.mkdirs();
		}
 
		System.out.println("Capture screen to " + extStorageDirectory + MyActivity.sRequireCaptureFilename + ".png");
		OutputStream outStream = null;
		File file = new File(extStorageDirectory, MyActivity.sRequireCaptureFilename + ".png");
		try {
			outStream = new FileOutputStream(file);
			bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
			outStream.flush();
			outStream.close();
		}
		catch(Exception e)
		{
			System.out.println("Fail to write screenshot");
			e.printStackTrace();
		}
 
		MyActivity.sRequireCapture = false;
	}
}
P/S (@atilim)
In onDrawFrame, how can I get screen-size, which return the same result as application:getDeviceWidth, application:getDeviceHeight ?

Likes: Teranth

+1 -1 (+1 / -0 )Share on Facebook

Comments

  • @Rickyngk Thanks for the code I am certainly going to have to try that one out and how it works.
    ThumbHurt Games / FB: ThumbHurt Games / FB: Eli/Teranth | Skype: teranth37
  • I wrote a simple android plugin, which use for setting value of MyActivity.sRequireCapture, MyActivity.sRequireCaptureWidth, MyActivity.sRequireCaptureHeight, MyActivity.sRequireCaptureFilename

    I need this feature because sometimes I have to capture bug on android phone (capture screen on android phone is not good like iOS).

    @atilim: In onDrawFrame, how can I get screen-size, which return the same result as application:getDeviceWidth, application:getDeviceHeight ?
  • I wrote a simple android plugin, which use for setting value of MyActivity.sRequireCapture, MyActivity.sRequireCaptureWidth, MyActivity.sRequireCaptureHeight, MyActivity.sRequireCaptureFilename

    I need this feature because sometimes I have to capture bug on android phone (capture screen on android phone is not good like iOS).

    @atilim: In onDrawFrame, how can I get screen-size, which return the same result as application:getDeviceWidth, application:getDeviceHeight ?
    Would it be possible that you share also the plugin with us, and/or an example project? Probably now i could do it for myself yet it would be just much simpler to have the plugin as well (maybe i'm speaking on behalf of others as well). Many thanks for the code in any case.
  • totebototebo Member
    Would love to get my hands on the plug too. Pretty please @Rickyngk?

    :)

    Niclas
    My Gideros games: www.totebo.com
  • SinisterSoftSinisterSoft Maintainer
    There is a plugin in Labs that captures screens on Android and iOS.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • Hi there, I used the media plugin from gideros labs to take screenshot.
    Unfortunately I always get black empty screenshots. What am I doing wrong?

    Here is a code sample I use:
    mediamanager:addEventListener(Event.MEDIA_RECEIVE, function(e)
        print(e.path)
        local path = e.path
        mediamanager:postPicture(path) -- to save it to the gallery, when I simply display the image at e.path, the result is the same.
        end)
      mediamanager:takeScreenshot()
  • ar2rsawseenar2rsawseen Maintainer
    @lsouchet I think the problem would be incorrect installation, in the point where you modify activity to capture OpenGL buffer
Sign In or Register to comment.