Mr. Mad
Mr. Mad

Reputation: 1238

How to share image Bitmap on facebook in android?

I am trying to share an image bitmap on facebook through my android app. I tried some codes found on google, but couldn't get success,here is my code

 shareButton = (ShareButton) findViewById(R.id.share_btn);

        //share dialog
        AlertDialog.Builder shareDialog = new AlertDialog.Builder(this);
        shareDialog.setTitle("Share Miimoji");
        shareDialog.setMessage("Share Miimoji to Facebook?");
        shareDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                //share the image to Facebook
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                mSelectedMiimoji.GetBitmap().compress(Bitmap.CompressFormat.PNG, 20, baos);
                SharePhoto photo = new SharePhoto.Builder().setBitmap(mSelectedMiimoji.GetBitmap()).build();
                SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
                shareButton.setShareContent(content);
                counter = 1;
                shareButton.performClick();
            }
        });
        shareDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        shareDialog.show();

please help me. Thanks in advance.

Upvotes: 1

Views: 1115

Answers (1)

Ядм Жцмдшдт
Ядм Жцмдшдт

Reputation: 110

try it once.

 public void ShareDialog() {
    Toast.makeText(Complete_Run_Activity.this, "Sharing to Facebook", Toast.LENGTH_SHORT).show();

    if (shareDialog.canShow(SharePhotoContent.class)) {

        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(bitmap)
                .setCaption("Hey... \nI earned by money by running/walking for the charity.")
                .build();
        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();
        shareDialog.show(content);
        dialog1.dismiss();
    }
}

also

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    try {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }catch(Exception e){
        e.printStackTrace();
    }}

and in oncreate method

 FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);
    shareDialog.registerCallback(callbackManager, new FacebookCallback<Sharer.Result>() {
        @Override
        public void onSuccess(Sharer.Result result) {
            Log.e("RESSULT", "Success");
         //   getAndAddSlots();

        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException error) {
            Log.e("RESSULT", "error=" + error.toString());
            Toast.makeText(Complete_Run_Activity.this, "Unable to Share Image to Facebook.", Toast.LENGTH_SHORT).show();
        }
    });

Upvotes: 1

Related Questions