0

我正在尝试使用 Graph api 在 Facebook 上发布图像。我已将图像转换为字节数组并尝试发布它,但我失败了。它显示了贴在墙上的文字,但图片没有发布。我不知道出了什么问题。

我已经检查了以下可用的 SO 解决方案,但没有一个对我有用。

1)如何使用图形 api 将可绘制文件夹中的图像发布到 facebook?

2)无法将图像从drawable发布到facebook

3)从android在facebook上发布带有文本的图像

4)使用图形 API 将 Android 照片上传到 Facebook?

这是我正在尝试的代码。

private void publishFeedDialog() {
        Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.ic_launcher);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] bitMapData = stream.toByteArray();

        final Bundle params = new Bundle();
        params.putString("name", "Test FB Post.");
        params.putString("method", "photos.upload");
        params.putByteArray("picture", bitMapData);

       //Tried below code but not working.          
        /*try {
            String response = mFacebook.request("me/photos", params, "POST");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        WebDialog feedDialog = (new WebDialog.FeedDialogBuilder(m_context,
                Session.getActiveSession(), params)).setOnCompleteListener(
                new OnCompleteListener() {

                    @Override
                    public void onComplete(Bundle values,
                            FacebookException error) {
                        if (error == null) {
                            // When the story is posted, echo the success
                            // and the post Id.
                            final String postId = values.getString("post_id");
                            if (postId != null) {
                                Toast.makeText(m_context,
                                        "Posted story, id: " + postId,
                                        Toast.LENGTH_SHORT).show();

                                finish();
                            } else {
                                // User clicked the Cancel button
                                Toast.makeText(m_context, "Publish cancelled",
                                        Toast.LENGTH_SHORT).show();
                            }
                        } else if (error instanceof FacebookOperationCanceledException) {
                            // User clicked the "x" button
                            Toast.makeText(m_context, "Publish cancelled",
                                    Toast.LENGTH_SHORT).show();
                        } else {
                            // Generic, ex: network error
                            Toast.makeText(getApplicationContext(),
                                    "Error posting story", Toast.LENGTH_SHORT)
                                    .show();
                        }
                    }

                }).build();
        feedDialog.show();
    }

请指导我。任何帮助将不胜感激。

谢谢

4

1 回答 1

0

最后我解决了我的问题,使用简单的 Facebook SDK for Android 在 Facebook 上共享图像,它包装了原始 Facebook SDK 3.5

这个 sdk 提供了使用引用SimpleFacebook类的资源在 facebook 墙上共享图像的简单方法。

您可以将照片发布(上传)到默认相册或您拥有的任何其他相册。照片可以从以下位置创建:

  • 位图
  • 文件
  • 字节[]
private SimpleFacebook mSimpleFacebook;
 mSimpleFacebook = SimpleFacebook.getInstance(this);
 Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.ic_launcher);
   // create Photo instace and add some properties
Photo photo = new Photo(bitmap);
photo.addDescription("Screenshot from sample application");
photo.addPlace("110619208966868");
// publish
mSimpleFacebook.publish(photo, new OnPublishListener()
{
    @Override
    public void onFail(String reason)
    {
    mProgress.hide();
    // insure that you are logged in before publishing
    Log.w(TAG, "Failed to publish");
    }
    @Override
    public void onException(Throwable throwable)
    {
           mProgress.hide();
        Log.e(TAG, "Bad thing happened", throwable);
    }
    @Override
    public void onThinking()
    {
     // show progress bar or something to the user while publishing
      mProgress = ProgressDialog.show(this, "Thinking",
    "Waiting for Facebook", true);
    }
    @Override
    public void onComplete(String id)
    {
        mProgress.hide();
        toast("Published successfully. The new image id = " + id);
    }
});
于 2013-10-22T10:05:13.447 回答