Android:在Facebook上发布图片

时间:2013-11-23 18:38:21

标签: android facebook

我正在尝试创建一个自动在用户的Facebook墙上发送图像的Android应用。我在网上看过很多例子(也来自这个网站),但显然似乎没有什么对我有用。这是我的代码:

class FacebookConnection extends AsyncTask<String, String, String> {

    private Exception exception;
    private Facebook facebook;
    private static String APP_ID = "574586632609202"; // Replace your App ID here
    Activity activity;

    public FacebookConnection(Activity activity)
    {
            this.activity = activity;
    }

    private void login()
    {
        facebook = new Facebook(APP_ID);
        facebook.authorize(activity, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},
                new DialogListener() {
            @Override
            public void onComplete(Bundle values) {
            }

            @Override
            public void onFacebookError(FacebookError error) {
            }

            @Override
            public void onError(DialogError e) {
            }

            @Override
            public void onCancel() {
            }
        });
    }

    protected String doInBackground(String... path) {
         login();
         Bundle parameters = new Bundle();
         parameters.putString("message", "ciao");

         Bitmap bi = BitmapFactory.decodeFile(path[0]);
         AsyncFacebookRunner mAsyncRunner = new   AsyncFacebookRunner(facebook);
         ByteArrayOutputStream stream = new ByteArrayOutputStream();
         bi.compress(Bitmap.CompressFormat.PNG, 100, stream);
         byte[] byteArray = stream.toByteArray();   
         parameters.putByteArray("picture", byteArray);

         String response = null;
         try {
                facebook.request("me");
             response = facebook.request("me/feed", parameters, "POST");
            } catch (MalformedURLException e) {
                return e.getMessage();
            } catch (IOException e) {
                return e.getMessage();
            }

         if (response == null || response.equals(""))
             return "No Response..";
         else 
           return  "Message has been posted to your walll!";
    }

    protected void onPostExecute(String mess) {
        show("DONE", mess);
    }
}

NB:我在按钮点击的主要活动中使用该类,我将我的主要活动作为参数传递。方法“show”只是在消息框中写入作为参数传递的字符串。 似乎登录还可以,但是当我检查我的Facebook墙时,没有任何改变。 谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:0)

<强>解决: 我解决了我的问题!!!这是完整的代码:

public class FacebookConnector {
    Facebook facebook;
    Context context;
    Activity activity;
    Handler mHandler;
    String[] permissions;
    String path;

    public FacebookConnector(String appId, Activity activity, String[] permissions) {
        this.facebook = new Facebook(appId);
        this.activity = activity;
        this.permissions=permissions;
        this.mHandler = new Handler();
    }

    public void setImagePath(String path) {
        this.path = path;
    }

    public void login() {
       facebook.authorize(this.activity, this.permissions, Facebook.FORCE_DIALOG_AUTH, new DialogListener() { 
           @Override
           public void onCancel() {
               Toast.makeText(activity, "Canceled", Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onComplete(Bundle values) {
               postImageonWall(); 
               Toast.makeText(activity, "Image Posted on Facebook.", Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onError(DialogError error) {
               Toast.makeText(activity, "Error", Toast.LENGTH_SHORT).show();
           }

           @Override
           public void onFacebookError(FacebookError fberror) {
               Toast.makeText(activity, "Facebook Error", Toast.LENGTH_SHORT).show();

           }

       });}

    public void postImageonWall() {             
        byte[] data = null;               

        Bitmap bi = BitmapFactory.decodeFile(path);
        //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);             
        ByteArrayOutputStream baos = new ByteArrayOutputStream();              
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);              
        data = baos.toByteArray();                
        Bundle params = new Bundle();              
        params.putString(Facebook.TOKEN, facebook.getAccessToken());              
        params.putString("method", "photos.upload");              
        params.putByteArray("picture", data);               
        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);              
        mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);    
    }

    public void execute() {
        login();
    }

    class SampleUploadListener implements AsyncFacebookRunner.RequestListener{

        @Override
        public void onComplete(String response, Object state) {
        }

        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }

    }
}

当然你必须先调用setImagePath方法来调用execute! 问候

弗朗西斯