2017-04-20 88 views
1

我想通過我的Android應用程序在Facebook上分享圖像位圖。我嘗試了一些谷歌代碼,但無法取得成功,這裏是我的代碼如何在Android上分享圖片在Facebook上的位圖?

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

請幫我。 在此先感謝。

+0

'試圖在Facebook上共享的圖像的位圖'。不要嘗試。 Facebook無法處理該位圖。如果您需要幫助,請不要事先感謝。 – greenapps

回答

1

嘗試一次。

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

@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(); 
    }} 

和在onCreate方法

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(); 
     } 
    }); 
相關問題