简体   繁体   中英

Posting a photo on user's facebook wall

Below is my code and I integrated the facebook in my app. I am trying to make the user to be able to post an image on his wall after logging in. But I have been failing to make it for three days. Have seen few posts like this but I don't get any data(just it results in empty data data[] ). I am even facing trouble with maintaining sessions too. Can some one please post working code or at least some sources where I can post image on facebook wall? Hope my below code is clear and my explanation is clear on what I have tried with the code below.

Activity's onCreate():-

public class FacebookActivity extends BaseActivity {
private static String APP_ID = "1303786178"; 


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fb_screen);

        btnFbLogin = (Button) findViewById(R.id.fbloginbtn);
        btnFbLogout = (Button) findViewById(R.id.fblogoutbtn);
        btnPostToWall = (Button) findViewById(R.id.fbpostbtn);
        msget = (EditText)findViewById(R.id.fbmsget);
        mAsyncRunner = new AsyncFacebookRunner(facebook);
         mHandler = new Handler();
        String passedpath = getIntent().getStringExtra("imagepath").toString();

        imageuri = Uri.parse(passedpath);

        /**
         * Login button Click event
         * */
        btnFbLogin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("Image Button", "button Clicked");
                loginToFacebook();
            }
        });

        btnFbLogout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("Image Button", "button Clicked");
                logoutFromFacebook();
            }
        });


        /**
         * Posting to Facebook Wall
         * */
        btnPostToWall.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //postToWall(imageuri);

                getAlbumId();
            }
        });


           }

Posting on Wall includes two functions in my code one is getAlbumId() and the other is posttowall() which will be called from getAlbumId():-

public void getAlbumId()
     {

         dialog = ProgressDialog.show(FacebookActivity.this, "","Please Wait...", true, true);

         //String wallAlbumID = null;
         mAsyncRunner.request("me/albums", new BaseRequestListener(){

            @Override
            public void onComplete(String response, Object state) {
                // TODO Auto-generated method stub
                try{
                Log.d("response in album"," "+response);
                JSONObject json = Util.parseJson(response);
                 JSONArray albums = json.getJSONArray("data");
                 for (int i =0; i < albums.length(); i++) {
                     JSONObject album = albums.getJSONObject(i);
                     if (album.getString("type").equalsIgnoreCase("wall")) {
                        final String wallAlbumID = album.getString("id");
                        globalalbid = wallAlbumID;
                         Log.d("JSON", wallAlbumID);
                         break;
                     }

                 }

                 dialog.dismiss();
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "id: " + globalalbid, Toast.LENGTH_LONG).show();
                            if(globalalbid != "")
                            postToWall(imageuri, globalalbid);
                        }
                    });

                }catch (JSONException e) {
                    e.printStackTrace();
                }

            }

         });

          }

function to post on wall:

public void postToWall(Uri locuri, String wallid) {

        if (facebook.isSessionValid()) {
            Util.showAlert(this, "Warning", "You must first log in.");
        } else {
            dialog = ProgressDialog.show(FacebookActivity.this, "","Please Wait...", true, true);


        // post on user's wall.
         Uri photoUri = locuri;
         if (photoUri != null) {
             Bundle params = new Bundle();
             try {
                 params.putByteArray("photo",
                         Utility.scaleImage(getApplicationContext(), photoUri));
             } catch (IOException e) {
                 e.printStackTrace();
             }
             String imagecaption = msget.getText().toString();
             if(imagecaption != "")
             params.putString("caption", imagecaption);
             mAsyncRunner.request(wallid+"/photos", params, "POST", new PhotoUploadListener(), null);
         } else {
             Toast.makeText(getApplicationContext(),
                     "Error from the pic", Toast.LENGTH_SHORT)
                     .show();
         }
        }
     } 


}

I always get album id empty. Is there anything wrong in the way that I try to get album id. Because I have seen on some posts those who try to access album id and then posting the image on album. They seem to be keeping wall as common findout in the album name. If it is so, some new ones contain timeline photos name, then how can we findout the album id. Please suggest.

Note: I want the image to be visible on to the wall not to only album.

Instead of uploading the photo to the user's album, post the image to /me/feed instead. Make sure you are asking the user for the publish_stream permission. This will upload the photo straight to the user's wall instead of going to an album.

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