从Facebook发布图像不起作用

时间:2013-01-14 05:29:59

标签: android facebook

我正在尝试在Android应用中的用户墙上发布带有标题的图像。但它不起作用。它不会在我的墙上显示任何内容。

这是我的代码:

    String APP_ID = " My ID";
                fb = new Facebook(APP_ID);
                sp = getPreferences(MODE_PRIVATE);
                access_token = sp.getString("access_token", null);
                long expires = sp.getLong("access_expires", 0);

                if(access_token!= null){
                    fb.setAccessToken(access_token);
                }

                if(expires != 0){
                    fb.setAccessExpires(expires);
                }


btnLogin.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                facebook.authorize(FbdemoActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                    }

                    @Override
                    public void onFacebookError(FacebookError error) {
                    }

                    @Override
                    public void onError(DialogError e) {
                    }

                    @Override
                    public void onCancel() {
                    }
                });

               posttowall();
            }
        });


public void postToWall() {
        // post on user's wall.
         try {
                if (fb.isSessionValid()) {
                    byte[] data = null;

                     Bitmap bi = BitmapFactory.decodeFile("/sdcard/Asa.jpg");
                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
                     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                     data = baos.toByteArray();


                     Bundle params = new Bundle();
                     params.putString(Facebook.TOKEN, fb.getAccessToken());
                     params.putString("message", "Test from Android AVD");
                     params.putString("method", "photos.upload");
                     params.putByteArray("picture", data);

                     AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(fb);
                     mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);

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

    }


public class SampleUploadListener extends BaseRequestListener {

        public void onComplete(final String response, final Object state) {
            try {
                // process the response here: (executed in background thread)
                Log.d("Facebook-Example", "Response: " + response.toString());
                JSONObject json = Util.parseJson(response);
                final String src = json.getString("src");

                // then post the processed result back to the UI thread
                // if we do not do this, an runtime exception will be generated
                // e.g. "CalledFromWrongThreadException: Only the original
                // thread that created a view hierarchy can touch its views."

            } catch (JSONException e) {
                Log.w("Facebook-Example", "JSON Error in response");
            } catch (FacebookError e) {
                Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            }
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {    
        }
    }

它没有显示任何错误,但它也没有发布任何内容。 任何人都可以指出我的代码中缺少什么或出了什么问题?

1 个答案:

答案 0 :(得分:0)

是的,我在您的代码中看到了一个问题 一个许可失踪..

  String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins",
        **"photo_upload"** };

添加此权限,然后检查...