简体   繁体   English

Facebook分享产品Android

[英]Facebook Sharing Product Android

I am working with the Facebook Sharing Product for Android. 我正在使用适用于Android的Facebook共享产品。 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. 链接和照片共享正在运行。现在,我的目标是从应用程序中获取带有其所有小部件的FrameLayout,将其转换为位图,然后在Facebook上共享。

I have found the following question and answers on Stack, and went to work with them. 我在Stack上找到以下问题和答案,并与他们一起工作。 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: 我在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. 我在“布局”文件中定义的宽度和高度均> 0,因此其他某些地方一定是错误的。

Any ideas? 有任何想法吗?

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. 出现此错误是因为方法“ createBitMap()”没有有效的参数,无法将布局的“宽度”和“高度”转换为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()". 我要做的是首先使用函数“ measure()”测量布局,然后在函数“ layout()”中声明布局,然后才将其称为“ 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM