【问题标题】:Facebook Sharing Product AndroidFacebook 分享产品 Android
【发布时间】:2018-04-23 14:19:56
【问题描述】:

我正在使用适用于 Android 的 Facebook 共享产品。链接和照片共享正在运行。现在我的目标是从应用程序中获取 FrameLayout 及其所有小部件,将其转换为位图,然后在 Facebook 上分享。

我在 Stack 上找到了以下问题和答案,并与他们一起工作。 Convert frame layout into image and save it

以下是我的代码:

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);

               }

}

我的 LogCat 中出现以下错误:

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

我在 Layout 文件中定义的宽度和高度都是 > 0,所以肯定有其他问题。

有什么想法吗?

【问题讨论】:

标签: android facebook-graph-api


【解决方案1】:

出现错误是因为方法“createBitMap()”没有有效参数用于将布局的宽度和高度转换为位图。

我所做的是首先使用函数“measure()”测量布局,然后在函数“layout()”中声明布局,然后才调用函数“createBitMap()”。下面的工作代码示例:

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);

【讨论】:

    猜你喜欢
    • 2019-04-15
    • 1970-01-01
    • 2018-09-30
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多