简体   繁体   中英

Android: post an image on facebook

I'm trying to create an android app that automatically sends an image on the facebook wall of the user. I've read a lot of examples on the net (also from this site) but apparently nothing seems to work for me. This is my code:

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: i use that class in the main activity on button click, i pass my main activity as parameter. The method "show" just write in a message box the string passed as parameter. It seems that the login is ok, but when i check my facebook wall, nothing has changed. Thank you for your help!!!

SOLVED: I've solved my problem!!! Here is the full code:

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) {
        }

    }
}

Of course you have to call the setImagePath method before to call the execute! Greetings

Francesco

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM