从Facebook不工作发布图片 [英] Posting Image from Facebook Not Working

查看:162
本文介绍了从Facebook不工作发布图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Andr​​oid应用程序,用户墙上张贴与标题的图像。但它不工作。这并不在我的墙上显示任何内容。

I'm trying to post an image with caption at the user wall in my Android App. But It's not working. It don't display anything on my wall.

下面是我的codeS:

Here is my Codes:

    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) {    
        }
    }

它不显示任何错误,但它也不发布任何东西。
任何人都能指出缺少了什么在我的code或有什么不好?

It doesn't show any error but it's not posting anything either. Can anyone point what's missing in my code or what is wrong ?

推荐答案

是的,我在你的code看到一个问题
权限为Permission丢失。

Yes i s seen one problem in your code one Permission missing..

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

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

Add this permission then check it...

这篇关于从Facebook不工作发布图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆