通过facebook sdk分享照片与描述从android应用程序到facebook墙

时间:2014-12-01 19:46:26

标签: android facebook

我想将带有描述的照片发布到Facebook墙上,就像" instagram"。我尝试了很多代码,但它们没用,有人能帮帮我吗? 我从另一个活动连接到Facebook,并将令牌传递给另一个活动发布照片,这是好的,但是帖子没有正常运行!! 我看到很多人在这个网站上谈论" AsyncTask"和听众?!

 public void UploadFacebookPhoto(){

            SharedPreferences prefs = getApplicationContext().getSharedPreferences("facebook", MODE_PRIVATE);
            String access_token = prefs.getString("access_token", null);
            long expires = prefs.getLong("access_expires", 0);
            fb.setAccessToken(access_token);
            fb.setAccessExpires(expires);

                if(access_token != null) {
                    fb.setAccessToken(access_token);
                    Toast.makeText(this, "facebook", Toast.LENGTH_SHORT).show();
                 }
                 if(expires != 0) {
                    fb.setAccessExpires(expires);
                    Toast.makeText(this, "facebook1", Toast.LENGTH_SHORT).show();
                 }
                 if (! fb.isSessionValid()) {
                    Toast.makeText(this, "facebook2", Toast.LENGTH_SHORT).show();
                 }
                 if (fb.isSessionValid()) {
                        Toast.makeText(this, "valid1", Toast.LENGTH_SHORT).show();
                   fb.authorize(HomeActivity.this, new String[]{ "publish_stream"},new DialogListener() {
                         @Override                     
                         public void onComplete(Bundle values) {
                             postToWall(values.getString(Facebook.TOKEN)); 
                             }                      
                         @Override                     
                         public void onFacebookError(FacebookError error) {
                         }                      
                         @Override                     
                         public void onError(DialogError e) {
                         }                      
                         @Override                     
                         public void onCancel() {

                         }                 
                     });
                 }
        }

       private void postToWall(String accessToken) {  

            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flower);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] data = stream.toByteArray();

            Bundle params = new Bundle();
            params.putString(Facebook.TOKEN, fb.getAccessToken());
            params.putString("caption", "flower");
            params.putByteArray("picture", data);
            try {
                fb.request("me/photos", params, "POST");

            } catch (FileNotFoundException fileNotFoundException) {
                Toast.makeText(HomeActivity.this,fileNotFoundException.getMessage(),Toast.LENGTH_LONG).show();
            } catch (MalformedURLException malformedURLException) {
                Toast.makeText(HomeActivity.this,malformedURLException.getMessage(),Toast.LENGTH_LONG).show();
            } catch (IOException ioException) {
                Toast.makeText(HomeActivity.this,ioException.getMessage(),Toast.LENGTH_LONG).show();
            }
        }//method

0 个答案:

没有答案