0

I am working with the Facebook Sharing Product for Android. Link and photo sharing are functioning.. Now my goal is to take a FrameLayout from the Application with all its Widgets, convert it to a Bitmap and then Share it on Facebook.

I have found the following question and answers on Stack, and went to work with them. Convert frame layout into image and save it

Following is my code:

public class LoginActivity extends AppCompatActivity {

    //callbackmanager manages callbacks to facebook sdk
    private CallbackManager callbackManager;
    private ShareButton framelayout_button;
    private FrameLayout frameLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        //STILL NEED TO INCLUDE THE PERMISSIONS SOMEWHERE..

        frameLayout = (FrameLayout) findViewById(R.id.framelayout_facebook);
        Bitmap bitmap = Bitmap.createBitmap(frameLayout.getWidth(), frameLayout.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        frameLayout.draw(canvas);

        SharePhoto frame_photo = new SharePhoto.Builder()
                .setBitmap(bitmap)
                .build();
        SharePhotoContent framelayout_content = new SharePhotoContent.Builder()
                .addPhoto(frame_photo)
                .build();

        framelayout_button = (ShareButton) findViewById(R.id.Sharebutton_FrameLayout);
        framelayout_button.setShareContent(framelayout_content);

               }

}

I get the following error in my LogCat:

java.lang.IllegalArgumentException: width and height must be > 0

My width and height defined in Layout file are both > 0, so something else must be wrong.

Any ideas?

Niels Vanwingh
  • 604
  • 1
  • 6
  • 22

1 Answers1

0

The error was present because the method "createBitMap()" did not have valid arguments for Width and Height of the Layout to convert to a BitMap.

What I did is first measure the Layout with the function "measure()", then declared the layout in function "layout()", and only then called the function "createBitMap()". Working code sample beneath:

public class LoginActivity extends AppCompatActivity {

//callbackmanager manages callbacks to facebook sdk
private CallbackManager callbackManager;
private ShareButton framelayout_button;
private FrameLayout frameLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    //STILL NEED TO INCLUDE THE PERMISSIONS SOMEWHERE..

    frameLayout = (FrameLayout) findViewById(R.id.framelayout_facebook);
    frameLayout.measure(frameLayout.getWidth(), frameLayout.getHeight());
    frameLayout.layout(0,0,frameLayout.getMeasuredWidth(),frameLayout.getMeasuredHeight());
    Bitmap bitmap = Bitmap.createBitmap(frameLayout.getWidth(), frameLayout.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    frameLayout.draw(canvas);

    SharePhoto frame_photo = new SharePhoto.Builder()
            .setBitmap(bitmap)
            .build();
    SharePhotoContent framelayout_content = new SharePhotoContent.Builder()
            .addPhoto(frame_photo)
            .build();

    framelayout_button = (ShareButton) findViewById(R.id.Sharebutton_FrameLayout);
    framelayout_button.setShareContent(framelayout_content);
Niels Vanwingh
  • 604
  • 1
  • 6
  • 22