facebook sdk:用文字分享本地图片让我发疯

时间:2014-08-02 14:20:37

标签: android facebook share local

目标

在我的相机应用程序中,我想让用户与预先格式化的文本和用户的Facebook墙上的描述共享图片。


INTRO

我用Google搜索了很多内容并跟随了#34;非常棒的" facebook-getting-started,尝试了许多事情,为!!天!! ...还没有找到完全可行的解决方案。

至少我认为我得到了一些观点:

  1. 伟大的Android Intent action_send效果不错但不选择适用于文字或图片的脸谱,不适用于两者(!!),请参阅here

  2. applicationID,hashxxx以及将Android应用程序链接到Facebook登录所需的一切,我真正理解并且不想做,完成和最后工作的所有事情(thanx facebook所有这些sh * t !!)

  3. 与facebook分享可以完成(见here):
    3A。使用Facebook应用程序(安装在设备中)
    3B。使用登录会话(如果未安装Facebook应用程序)

  4. Facebook希望我们使用其SDK,它是非常[b | s]的广告,但我能忍受它。

  5. 案例3a - facebook建议我们使用shareDialog,并且使用代码片段和建议的示例进行大量的战斗! - 我已经能够做到了,但是如果facebook应用程序不是,我也需要它已安装(案例3b。)

  6. 案例3b - facebook建议使用" ugly" feedDialog作为后备(见here) 6A。 feedDialog需要登录才能正常...
    6B。似乎feedDialog无法分享本地图像,我真的不明白为什么... here Facebook指南只谈及" URL"对于"图片"关键字...
    5C。然后我认为我必须使用Request,我尝试实现它但没有发生任何事情,我想我错过了一些东西......没有找到有用的参考文献(=工作实例)。


  7. 来自SO和developers.facebook的 CODE SNIPPETS 复制/粘贴/编辑

    Sharedialog和会话管理,以防未安装facebook app IT' S WORKING

    /**
     * share image with facebook, using facebook sdk
     * @param pathToImage
     */
    private void shareWithFacebook(String pathToImage) {
        Log.i("SHARE", "share with facebook");
        if (FacebookDialog.canPresentShareDialog(getApplicationContext(), FacebookDialog.ShareDialogFeature.SHARE_DIALOG)) {
            // Publish the post using the Share Dialog
            Log.i("SHARE", "share with ShareDialog");
            FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
            .setCaption("Sharing photo taken with MyApp.")
            .setName("Snail Camera Photo")
            .setPicture("file://"+pathToImage)
            .setLink("http://myapplink")
            .setDescription("Image taken with MyApp for Android")
            .build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else {
            // maybe no facebook app installed, trying an alternative
            // here i think i need a session
            if (Session.getActiveSession() != null && Session.getActiveSession().isOpened()) {
                publishFeedDialog(pathToImage);
            } else {
                Session session = Session.getActiveSession();
                if (!session.isOpened() && !session.isClosed()) {
    
                    List<String> permissions = new ArrayList<String>();
                    permissions.add("publish_actions");
    
                    session.openForRead(new Session.OpenRequest(this)
                        .setPermissions(permissions)
                        .setCallback(mFacebookCallback));
                } else {
                    Session.openActiveSession(this, true, mFacebookCallback);
                }
            }
        }
    }
    
    private Session.StatusCallback mFacebookCallback = new Session.StatusCallback() {
        public void call(final Session session, final SessionState state, final Exception exception) {
            if (state.isOpened()) {
                String facebookToken = session.getAccessToken();
                Log.i("SHARE", facebookToken);
                Request.newMeRequest(session, new Request.GraphUserCallback() {
                    public void onCompleted(GraphUser user, com.facebook.Response response) {
                        publishFeedDialog(MP.LAST_TAKEN_FOR_GALLERY);
                    }
                }).executeAsync();
            }
        }
    };
    



    feedDialog片段,适用于文本字段,链接和远程网址,但使用&#34;适用于本地图片使用&#34; file://...&#34;也不是&#34; file:///..."。错误消息:picture URL is not properly formatted

    private void publishFeedDialog(String pathToImage) {
        Bundle params = new Bundle();
        params.putString("name", "myapp Photo");
        params.putString("caption", "Sharing photo taken with myapp.");
        params.putString("description", "Image taken with myapp for Android");
        params.putString("link", "http://myapp.at.playstore");
        params.putString("picture", "file://"+pathToImage);
    
    
        WebDialog feedDialog = (
            new WebDialog.FeedDialogBuilder(EnhancedCameraPreviewActivity.this,//getApplicationContext(),
                Session.getActiveSession(),
                params))
            .setOnCompleteListener(new OnCompleteListener() {
    
                public void onComplete(Bundle values,
                    FacebookException error) {
                    Log.i("SHARE", "feedDialog.onComplete");
                    if (error == null) {
                        // story is posted
                        final String postId = values.getString("post_id");
                        if (postId != null) {
                            Toast.makeText(getApplicationContext(),
                                "Posted story, id: "+postId,
                                Toast.LENGTH_SHORT).show();
                        } else {
                            // User clicked the Cancel button
                            Toast.makeText(getApplicationContext(), 
                                "Publish cancelled", 
                                Toast.LENGTH_SHORT).show();
                        }
                    } else if (error instanceof FacebookOperationCanceledException) {
                        // User clicked the "x" button
                        Toast.makeText(getApplicationContext(), 
                            "Publish cancelled", 
                            Toast.LENGTH_SHORT).show();
                    } else {
                        // Generic, ex: network error
                        Toast.makeText(getApplicationContext(), 
                            "Error posting story", 
                            Toast.LENGTH_SHORT).show();
                    }
                }
    
            })
            .build();
        feedDialog.show();
    }
    



    另一个尝试,使用请求,它说:

      

    照片上传问题。错误= {HttpStatus:403,errorCode:200,   errorType:OAuthException,errorMessage:(#200)需要扩展   permission:publish_actions}

    我尝试在上面的会话管理中添加publish_actions权限,也许我想念一些......

    private void publishFeedDialog(String pathToImage) {
        Request request=Request.newPostOpenGraphObjectRequest(
                Session.getActiveSession(),
                "PhotoUpload",
                "myApp Photo Upload",
                "file://"+pathToImage,
                "http://myapp.at.playstore",
                "Image taken with myApp for Android",
                null,
                uploadPhotoRequestCallback);
        request.executeAsync();
    }
    



    最后尝试请求,绝对没有任何反应,无论是否有&#34;图片&#34;关键字。

    private void publishFeedDialog(String pathToImage) {
        Bundle parameters = new Bundle();
        parameters.putString("message", "Image taken with myApp for Android");
        parameters.putString("picture", "file://"+pathToImage);
        parameters.putString("caption", "myApp Photo");
    
        Request request = new Request(Session.getActiveSession(), "/me/feed", parameters, null);
        // also tried: Request request = new Request(Session.getActiveSession(), "/me/feed", parameters, com.facebook.HttpMethod.POST);
        request.executeAsync();
    }
    



    问题

    -1-我的1分6分中是否有一些错误?

    -2-我可以使用FeedDialog分享本地图片吗?

    -3-如果没有,如何安装facebook app?

    非常感谢!

1 个答案:

答案 0 :(得分:1)

我遇到同样的问题,但我有一个解决方法。

  1. 在你的观点1-6中我找不到任何错误的推理,看起来像是我遇到过的相同问题。
  2. 您无法在本地分享图片,,但......
  3. 以下代码段:
  4. 这会将图片上传到用户个人资料并将其张贴在他/她的墙上,之后您可以根据需要获取该网址:

    private void uploadPicture(final String message, Session session) {
            Request.Callback uploadPhotoRequestCallback = new Request.Callback() {
                @Override
                public void onCompleted(com.facebook.Response response) {
                    if (response.getError() != null) { 
                         Toast.makeText(getActivity(), "Failed posting on the Wall", Toast.LENGTH_LONG).show();
                        return;
                    }
    
                Object graphResponse = response.getGraphObject().getProperty("id");
                if (graphResponse == null || !(graphResponse instanceof String) ||
                        TextUtils.isEmpty((String) graphResponse)) { 
                    Toast.makeText(getActivity(), "Failed uploading the photo\no respons", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity(), "Succsefully posted on the facebook Wall", Toast.LENGTH_LONG).show();
                }                
            }
        };
    
        // Execute the request with the image and appropriate message
        Request request = Request.newUploadPhotoRequest(session, profileBitmap, uploadPhotoRequestCallback);
        Bundle params = request.getParameters();
        params.putString("message", message);
        request.executeAsync();
    }
    

    当你没有APP时启动facebook登录,它更简单,我使用以下代码片段在facebook按钮上调用facebook SDK:

    @Override
    public void onClick(View v) {
            switch (v.getId()) {
                 case R.id.faceBookButton:
                            if (((imagePath != null && !imagePath.equals("")) || (message != null && !message.equals("")))) {
                                Session session = Session.getActiveSession();
                                if (!session.isOpened() && !session.isClosed()) {
                                    session.openForPublish(new Session.OpenRequest(this)
                                            .setPermissions(Arrays.asList("public_profile", "publish_actions"))
                                            .setCallback(statusCallback));
                                } else {
                                    if (profileBitmap != null) {
                                        uploadPicture(message, session);
                                    } else {
                                        publishFeedDialog(message);
                                    }
                                }
                            } else {
                                Toast.makeText(getActivity(), getString(R.string.checkin_fail), Toast.LENGTH_SHORT).show();
                            }
                      break;
               }      
         }
    

    在我的情况下,publishFeedDialog接受您要传递的消息,而不是图像,但这并不重要。无论如何,它打开了Facebook的消息对话框,我还没有找到从EditText小部件传递预定义消息的方法。