Search code examples
androidlibgdxscreenshotscreensharing

How to CaptureScreen shot of current screen to share score like temple run game in android libgdx game gives only black screen


I want to post a screen shot of my game on social apps. I have developed this game using Libgdx framework. I have created an interface in Core class. Here is its Intent share specific code.

public interface ActionResolver {
   public void shareit();

   }

then on my Androidlauncher class I implement it

public class AndroidLauncher extends AndroidApplication implements
        ActionResolver{ .......

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

gameView = initializeForView(new MainGame(this),
                new AndroidApplicationConfiguration());// View
}

 @Override public void shareScreen() {

      String message = "Text I want to share."; Intent share = new
     Intent(Intent.ACTION_SEND); share.setType("text/plain");
      share.putExtra(Intent.EXTRA_TEXT, message);

      startActivity(Intent.createChooser(share,
      "Title of the dialog the system will open"));

      }


     public void createIntent(View v){

      { // View view =gameView.getRootView(); Bitmap icon =
      getBitmapFromView(v.getRootView()); Intent share = new
      Intent(Intent.ACTION_SEND); share.setType("image/jpeg");

      ContentValues values = new ContentValues();
      values.put(MediaStore.Images.Media.TITLE, "title");
      values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); Uri uri =
      getContentResolver().insert(
      MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

      OutputStream outstream;

      try { outstream = getContentResolver().openOutputStream(uri);
      icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
      outstream.close(); } catch (Exception e) {
      System.err.println(e.toString()); }

      share.putExtra(Intent.EXTRA_STREAM, uri);
      startActivity(Intent.createChooser(share, "Share Image")); } }

public void createIntent(View v){

      Bitmap icon = getBitmapFromView(v.getRootView()); Intent share = new
      Intent(Intent.ACTION_SEND); share.setType("image/jpeg");

      ContentValues values = new ContentValues();
      values.put(MediaStore.Images.Media.TITLE, "title");
      values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); Uri uri =
      getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
      values);


}
public static Bitmap getBitmapFromView(View view) {

        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),
                view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);
        return returnedBitmap;
    }

I also Initialize my Interface in MainGame class

public MainGame(ActionResolver actionResolver) {
        super();
        this.actionResolver = actionResolver;

One thing I am confuse Which view I should pass to start this intent? becuase its libgdx framework. Secondly on Android side obviously Canvas, Bitmap all would be from Android.

with this whole code I am able to start my intent but when i share screen the screen is just a black screen. but That screen has an ad appears on its bottom that I am already using through admob.

I have searched it here a lot, found related thread of sharing a black screen instead of actual screen shot of game. but I dont Understand all that because some asking to use some different library. I dont wana use any external library nor facebook sdk for facebook. I just want simple android Intent to share Screen . Kindly help me out.


Solution

  • com.badlogic.gdx.utils.ScreenUtils.getFrameBufferPixmap works for me.

    https://github.com/libgdx/libgdx/wiki/Take-a-Screenshot