2

my english is not so perfect but i want to learn, so i hope you can understand me. I wanna know how can i change the facebook profile pic using facebook android sdk. im able to upload pictures from my app to any album, including "Profile Pictures" (i use the next code:)

params=new Bundle();
params.putByteArray("photo", photo);
params.putString("caption", description);
mAsyncRunner.request(idAlbum+"/photos", params,"POST",new RequestListener() {

                    @Override
                    public void onMalformedURLException(MalformedURLException e, Object state) {}
                    @Override
                    public void onIOException(IOException e,Object state) {}
                    @Override
                    public void onFileNotFoundException(FileNotFoundException e, Object state){}
                    @Override
                    public void onFacebookError(FacebookError e,Object state) {}
                    @Override
                    public void onComplete(String response, Object state) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(),"Success", Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                }, null);

but i dont know how to set the uploaded image as the profile pic. maybe adding some code to this? I know that what i ask is posible using facebook sdk for php. also, i have seen an Apple app that do this too. Then, i think i can do it in android. somebody has information about what im looking for? thanks in advance.

Max Collao
  • 91
  • 1
  • 12
  • 1
    _“I wanna know how can i change the facebook profile pic”_ – you _can not_ change the profile pic via API _at all_ – the user has to do this manually. – CBroe Sep 04 '12 at 11:28
  • @CBroe my friend what i wanna do is create an app where the user can choose a photo from his phone gallery, select which album he/she wants to upload it, and if is Profile Picture album ask her/him if he wants to set the photo as his/her profile pic. My friend this, yes, is posible. As I said, I know about an app that can do this without any Facebook policy violation. Also, I have seen php code that works perfectly doing this. that's the reason that I'm asking about this. – Max Collao Sep 05 '12 at 21:57
  • So what’s this “PHP code” that’s doing this? If you can show a _working_ PHP example, then maybe we can figure out what the correct API call for this feature would be. – CBroe Sep 06 '12 at 13:07
  • check this [link](http://4rapiddev.com/facebook-graph-api/php-change-facebook-profile-picture-with-graph-api/) – Max Collao Sep 06 '12 at 22:06
  • 1
    So, as I already said, it can’t be done via the Graph API, and the page you linked to only confirms that. This script does the trick, by redirecting the user to a special URL on Facebook, that makes the given picture the profile picture. So, if you want to do the same thing: Provided that solution still works (I don’t know, the article is from 2011), you would have to open a browser control from your app and have that URL loaded into there. (And you would have to hope that the user is logged in to Facebook via web browser on his device as well, because otherwise it will certainly not work.) – CBroe Sep 07 '12 at 07:55

1 Answers1

4

Ok, I found the way to do that. and this is the code:

params=new Bundle();
    try {
        params.putByteArray("photo", photo);
    } catch (IOException e) {
        e.printStackTrace();
    }
    params.putString("caption", description);

mAsyncRunner.request(idAlbum+"/photos", params,"POST",
                        new RequestListener() {
                    @Override
                    public void onMalformedURLException(MalformedURLException
                            e, Object state) {}
                    @Override
                    public void onIOException(IOException e,Object state) {}
                    @Override
                    public void onFileNotFoundException(
                            FileNotFoundException e, Object state){}
                    @Override
                    public void onFacebookError(FacebookError e,Object state) {}

                    @Override
                    public void onComplete(final String response, Object state) {
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    JSONObject json=new JSONObject(response);
                                    JSONObject obj=new JSONObject(
                                            facebook.request("me"));
                                    final String photoId=json.getString("id");
                                    String userId=obj.getString("id");

                                    String url="https://m.facebook.com/photo." +
                                            "php?fbid="+photoId+"&id="+userId+
                                            "&prof&__user="+userId;
                                    Intent mIntent=new Intent(Intent.ACTION_VIEW,
                                    Uri.parse(url));
                                    startActivity(mIntent);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }

                            }
                        });
                    }
                }, null);

Post the photo on a specific album, get the photoID with a JSONObject, and then redirect to a web page where the user can confirm to set the photo as his/her profile picture.

Max Collao
  • 91
  • 1
  • 12