Android : Taking screenshot and sharing it through shareIntent in andengine

on Friday, October 31, 2014


Hi I am making a game where there is a share button. If a user clicks it, it will take the current screen as a PNG image and share it through share intent. I am able to take the screenshot and save it to storage and then I wish to invoke the share intent through startActivity(). I have two functions for that: captureImage() will save the screenshot, then I am calling shareImage() to invoke the startActivity(). My problem is whenever I click on the button it invokes the shareActivity() first and then tries to capture the screenshot. I mean they are running in two different threads, while a thread tries to capture image, another thread invokes startActivity(), so it tries to collect the image from the predefined path before it is actually saved. Here I am facing the problem as its always giving me the error. Following are my codes: I put two different toasts to find out which one is running first. I found shareImage() toast is always coming first. I am trying to find a way to make shareImage() run until captureImage() is finished.



final Sprite sShare = new Sprite(Constants.CAMERA_WIDTH - 50 , 20, this.mShare, this.vertexBufferObjectManager) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {
captureImage();
shareImage();
}
return true;
}
};


My two methods are as follow:



private void captureImage() {
final int viewWidth = MenuActivity.this.mRenderSurfaceView.getWidth();
final int viewHeight = MenuActivity.this.mRenderSurfaceView.getHeight();
screenCapture.capture(viewWidth, viewHeight, FileUtils.getAbsolutePathOnExternalStorage(MenuActivity.this, "screenshot.png"), new IScreenCaptureCallback() {
@Override
public void onScreenCaptured(final String pFilePath) {
MenuActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MenuActivity.this, "Screenshot: " + pFilePath + " taken!", Toast.LENGTH_SHORT).show();
}
});
}

@Override
public void onScreenCaptureFailed(final String pFilePath, final Exception pException) {
MenuActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MenuActivity.this, "FAILED capturing Screenshot: " + pFilePath + " !", Toast.LENGTH_SHORT).show();

}
});
}
});
}

private void shareImage() {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/png");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(FileUtils.getAbsolutePathOnExternalStorage(MenuActivity.this, "screenshot.png")));
MenuActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MenuActivity.this, "Path: " + Uri.parse(FileUtils.getAbsolutePathOnExternalStorage(MenuActivity.this, "screenshot.png")), Toast.LENGTH_SHORT).show();
}
});
startActivity(Intent.createChooser(shareIntent, "Share via"));
}


Thank you in advance.


0 comments:

Post a Comment