Crash on Android when sharing screenshot using Lua

I am working on a game using cocos2d-x 3.1.1 + Lua, for Android. But that game crash when sharing screenshot. Below are some code snippets:

-- take screenshot using Lua
--
local size = cc.Director:getInstance():getVisibleSize()
local texture = cc.RenderTexture:create(size.width, size.height)
texture:setPosition(size.width / 2, size.height / 2)

texture:begin()
cc.Director:getInstance():getRunningScene():visit()
texture:endToLua()
texture:saveToFile(filename, cc.IMAGE_FORMAT_PNG)

Then I send the file to Java via LuaJavaBridge. Java code do below things:

  1. Copy the image from private folder to sdcard
  2. creates an Intent of ACTION_SEND from the copied image, and call startActivity()

I upload the apk to phone. When I tap the share button, a share chooser dialog pops up. Then I tap Cancel to dismiss the dialog, app crashes and go back to desktop.

Some facts may help to locate issue:

  1. Screenshot is taken correctly. I can see the screenshot file in sdcard.
  2. I can share if comment out above Lua code, which is used to take screenshot.

Someone can help on this?

Thank you

I use the lua-tests/NewEventDispatcher/PauseResumeTargetTest to check this,I insert the following code in the ‘popup’ funciton:

        local size = cc.Director:getInstance():getVisibleSize()
        local texture = cc.RenderTexture:create(size.width, size.height)
        texture:setPosition(size.width / 2, size.height / 2)
        texture:begin()
        cc.Director:getInstance():getRunningScene():visit()
        texture:endToLua()
        texture:saveToFile("image-0.png", cc.IMAGE_FORMAT_PNG)

Then, I tap Popup to pop up a layer,then i tap close to exit layer.The test didn’t crash,can you pasts more codes?
And, The v3.2 add the CaptureScreenTest,you can also refer to it.

Thanks. I still don’t get the reason why it crashes. It triggers an exception in libc, signal 11.
Will try it on 3.2.

In Java Code
First you must to get the main looper

mHandler = new Handler(this.getMainLooper());

And then start a activity use the mHandler:

mHandler.post(new Runnable() {
	
	@Override
	public void run() {
		String path = "/mnt/sdcard/elevengames/";
		String imagePath = path + "screenshot.png";
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdir();
		}
		File imageFile = new File(imagePath);
		try {
			imageFile.createNewFile();
			FileInputStream is = mContext.openFileInput("screenshot.png");
			FileOutputStream os = new FileOutputStream(imageFile);
			byte[] buffer = new byte[1024];
			int count = 0;
			while((count = is.read(buffer)) != -1) {
				os.write(buffer);
				os.flush();
			}
			os.close();
			is.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("image/*");
		Uri uri = Uri.fromFile(imageFile);
		shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
		mContext.startActivity(shareIntent);
	}
});