如何使用FB Android API将LibGDX屏幕截图分享到Facebook?

时间:2018-09-30 15:48:06

标签: java android facebook libgdx facebook-android-sdk

我想使用Facebook Android SDK将libGDX屏幕共享到Facebook。我的代码是:

AndroidLauncher.java

if (!fbLoggedin()) fbLogin();
SharePhotoContent content = new SharePhotoContent.Builder()
  .addPhoto(new SharePhoto.Builder()
    .setBitmap(Utils.takeScreenShot(graphics.getView()))
    .build())
  .setShareHashtag(new ShareHashtag.Builder()
    .setHashtag("#enibo")
    .build())
  .build();
shareDialog.show(content, ShareDialog.Mode.AUTOMATIC);

Utils.java

class Utils {
    ...
    public Bitmap takeScreenShot(View view) {
        view.setDrawingCacheEnabled(true);
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_LOW);
        view.buildDrawingCache();
        if(view.getDrawingCache() == null) return null;
        Bitmap snapshot = Bitmap.createBitmap(view.getDrawingCache());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
        return snapshot;
    }
}

但是我在Facebook分享对话框上只看到黑色图像。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

  

我曾经做过这样的事情。这是我使用的代码段。

定义视图

screenshot = (CardView) findViewById(R.id.screenshot);
screenshot.setDrawingCacheEnabled(true);
screenshot.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

采用此方法创建视图的位图

public static Bitmap loadBitmapFromView(View v) {
    Bitmap b = Bitmap.createBitmap( v.getMeasuredWidth(),v.getMeasuredHeight() , Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
    v.draw(c);
    return b;
}

然后保存到存储中并使用它(请参阅uri)

try {
    loadBitmapFromView(screenshot)
            .compress(Bitmap.CompressFormat.JPEG,
                    100,
                    new FileOutputStream("/storage/emulated/0/image.jpg"));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
Uri uri = Uri.fromFile(new File("/storage/emulated/0/image.jpg"));