Android无法使用facebook sdk将图片发布到墙上

时间:2012-01-11 03:23:45

标签: android facebook facebook-graph-api

我有问题用facebook sdk到墙上画。以下是我的代码。

活动开始时的Facebook权限对话框:

fb = new Facebook(fb_id);

fb.authorize(this, new String[] { "publish_stream",
                "read_stream", "offline_access","read_friendlists"}, new DialogListener(){

public void onComplete(Bundle values) {
    Log.e("oncomplete","value");
    if (values.isEmpty()) {
        Log.e("oncomplete","value is empty");
        return;
    }
    if (!values.containsKey("POST")) {
        token = fb.getAccessToken();
    }
}
public void onFacebookError(FacebookError e) {}
public void onError(DialogError e){}    
public void onCancel() {}
});

点击按钮向墙发送Feed

public void onClick(View v){
if(v==post_to_wall){
Toast.makeText(getApplicationContext(), "fb token " + token, Toast.LENGTH_LONG).show();
final Handler handler = new Handler(){
        public void handleMessage(Message what){
            finish();
        }
    };
    Thread thread = new Thread(){
        public void run(){
            publishToFriendsWall(friends_id);   
            handler.sendEmptyMessage(0);
        }
    };
    thread.start();             
    }
}

发布到朋友墙方法

public void publishToFriendsWall(String friends_id){
        if(fb.isSessionValid()){
        try{                                
            Bundle params = new Bundle();
            params.putString(Facebook.TOKEN, token);
                params.putString("message", edittext.getText().toString());
                params.putString("link", "http://www.mysite.com");
            params.putString("caption", "my caption");
            params.putString("description", "description of my link.  Click the link to find out more.");
            params.putString("name", "my name");
            //params.putString("picture", "mypic url");

           fb.request(((friends_id == null) ? "me" : friends_id) + "/feed", params, "POST");
        }catch(Exception e){
            Log.d(null, "Fb post to wall error occured " + e.getClass().getName());
        }               
    }
    }

问题是我把以下代码

params.putString("picture", "mypic url");

没有任何内容发布到墙上,如果我评论上面的代码它工作正常,我通过facebook图形api文档阅读发布图片它是必需的访问令牌和图片的有效网址。我通过获取访问令牌完成了所有这些,并且url是有效的。我的代码出了什么问题?也没有发现任何错误。我在logcat中唯一得到的就是警告

Key picture expected byte[] but value was a java.lang.String. the default value <null> was return
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.String
    at android.os.Bundle.getByteArray(Bundle.java:1305)
    at com.facebook.android.Util.encodePostBody(Util.java:63)
    at com.facebook.android.Util.openUrl(Util.java:182)
    at com.facebook.android.Facebook.request(Facebook.java:563)

1 个答案:

答案 0 :(得分:0)

mFacebook.setAccessToken(access_token);
    boolean validity=mFacebook.isSessionValid();

    try
    {
    byte[] data = null;

    BitmapFactory.Options opts = new BitmapFactory.Options();

     opts.inSampleSize=4;           
     Bitmap bi = BitmapFactory.decodeFile(st_imagepath,opts);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
     data = baos.toByteArray();


      Bundle params = new Bundle();
      params.putString(Facebook.TOKEN, mFacebook.getAccessToken());
      params.putString("method", "photos.upload");
      params.putString("caption", caption);



      params.putByteArray("picture", data);

      AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(mFacebook);

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

使用此工作代码。 Ur问题将得到解决