我正在使用适用于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
我在布局文件中定义的宽度和高度都是> 0,所以别的东西一定是错的。
有什么想法吗?
答案 0 :(得分:0)
出现错误是因为方法" createBitMap()"没有有效的参数,用于转换为BitMap的布局的宽度和高度。
我所做的是首先使用函数" measure()"来测量布局,然后在函数" layout()"中声明布局,然后才调用函数" createBitMap()&#34 ;.下面的工作代码示例:
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);