简体   繁体   中英

How to share LibGDX screenshot to Facebook using FB Android API?

I want to share libGDX screen to Facebook using Facebook Android SDK. My code is:

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

But I only see a black image on the Facebook Share Dialog. How can I fix this issue?

I once did something like this. Here are the code snippets I used.

Define the view

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

Take this method to create Bitmap of your view

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

Then save to storage and use it (see 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"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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