简体   繁体   中英

how to change the facebook profile picture using facebook android sdk?

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.

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.

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