android facebook api post to wall with image

前端 未结 2 569
滥情空心
滥情空心 2021-02-02 02:50

I would like to be able to use the facebook android sdk and post a link to facebook. An example of what I want would be is if you were on facebook and you type a link into your

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-02 03:33

    This works perfect fine with Progress Dialog box.. I have used it...

    You must added the jar of Facebook...

      Facebook authenticatedFacebook = new Facebook(APP_ID);
    
      private static final String[] PERMISSIONS = new String[] { "publish_stream", "read_stream", "offline_access" };
    

    Call below function on button Click....

      authenticatedFacebook.authorize(YOUR_CLASS_NAME.this, PERMISSIONS, new FaceBookWallPostListener());
    

    Now Add this class...

      public class FaceBookWallPostListener implements DialogListener {
    
        public void onComplete(Bundle values) {
            new FacebookWallPost().execute();
        }
    
        public void onCancel() {
        }
    
        public void onError(DialogError e) {
            e.printStackTrace();
        }
    
        public void onFacebookError(FacebookError e) {
            e.printStackTrace();
        }
    }
    
    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
    
    
    
    
    
     private class FacebookWallPost extends AsyncTask {
    
        @Override
        protected String doInBackground(String... params) {
            try {
                path = "Path OF YOUR IMAGE";
                Bundle parameters = new Bundle();
                parameters.putString("message", "MESSAGE YOU WANT TO POST");
                try {
                    File file = new File(path, "IMAGE_NAME.jpg");
                    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                    byte[] data = null;
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
                    data = baos.toByteArray();
                    if (data != null) {
                        parameters.putByteArray("picture", data);
                    }
                    parameters.putString("access_token", authenticatedFacebook.getAccessToken());
                    authenticatedFacebook.request("me");
                    authenticatedFacebook.request("me/photos", parameters, "POST");
                } catch (Exception e) {
                    return e.getMessage();
                }
    
                return "success";
            } catch (Exception e) {
                return e.getMessage();
            }
        }
    
        @Override
        protected void onPostExecute(String result) {
            pDialog.dismiss();
            if (result.equals("success")) {
                Toast.makeText(YOUR_CLASS_NAME.this, "WallPost Successfully Done", Toast.LENGTH_SHORT).show();
                try {
                    new File(Environment.getExternalStorageDirectory().toString() + "/Diegodeals", "diegodeals.jpg").delete();
                } catch (Exception e) {
                }
    
            } else {
                Toast.makeText(YOUR_CLASS_NAME.this, "Failed to post \n " + result, Toast.LENGTH_SHORT).show();
            }
    
        }
    
        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(YOUR_CLASS_NAME.this);
            pDialog.setMessage("Posting Picture & Message on Facebook...");
            pDialog.show();
        }
    
    }
    

    /////GOOOD LUCK.

提交回复
热议问题