我正在尝试将图片张贴到Facebook的墙上。我已成功登录并在墙上张贴文字。但是,当我尝试发布图片时,没有任何反应。
我正在使用Android Facebook SDK。
这是我到目前为止的内容:
Bundle params = new Bundle(); params.putString("method", "photos.upload"); Toast.makeText(FacebookPoster.this, "" + getIntent().getExtras().getByteArray("data").length, Toast.LENGTH_SHORT).show(); params.putByteArray("picture", getIntent().getExtras().getByteArray("data")); AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook); mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);
Toast显示8733,这意味着字节数组不为空
注意 Logcat输出一些警告(不是错误):
03-02 14:19:29.554: WARN/Bundle(1891): Attempt to cast generated internal exception: 03-02 14:19:29.554: WARN/Bundle(1891): java.lang.ClassCastException: java.lang.String 03-02 14:19:29.554: WARN/Bundle(1891): at android.os.Bundle.getByteArray(Bundle.java:1305) 03-02 14:19:29.554: WARN/Bundle(1891): at com.facebook.android.Util.openUrl(Util.java:155) 03-02 14:19:29.554: WARN/Bundle(1891): at com.facebook.android.Facebook.request(Facebook.java:559) 03-02 14:19:29.554: WARN/Bundle(1891): at com.facebook.android.AsyncFacebookRunner$2.run(AsyncFacebookRunner.java:253) 03-02 14:19:29.584: WARN/Bundle(1891): Key method expected byte[] but value was a java.lang.String. The default value <null> was returned.
(在彼此下方显示几次。)
我究竟做错了什么?
解决了。 这是我所做的,以使其正常工作:
facebook.authorize(this, new String[] { "publish_stream" }, new DialogListener() { @Override public void onFacebookError(FacebookError e) { // TODO Auto-generated method stub } @Override public void onError(DialogError dialogError) { // TODO Auto-generated method stub } @Override public void onComplete(Bundle values) { postToWall(values.getString(Facebook.TOKEN)); } @Override public void onCancel() { // TODO Auto-generated method stub } });
和辅助方法:
private void postToWall(String accessToken) { Bundle params = new Bundle(); params.putString(Facebook.TOKEN, accessToken); // The byte array is the data of a picture. params.putByteArray("picture", getIntent().getExtras().getByteArray("data")); try { facebook.request("me/photos", params, "POST"); } catch (FileNotFoundException fileNotFoundException) { makeToast(fileNotFoundException.getMessage()); } catch (MalformedURLException malformedURLException) { makeToast(malformedURLException.getMessage()); } catch (IOException ioException) { makeToast(ioException.getMessage()); } }
首先是您没有使用图形API上传图片…您使用的是旧的REST API …请尝试使用图形API,它非常简单…
使用以下代码:
Bundle param = new Bundle(); param.putString("message", "picture caption"); param.putByteArray("picture", ImageBytes); mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());
根据错误消息,在从intent的包中获取字节时,它似乎给出了错误信息。