上传图片/照片/图片facebook(android)

时间:2011-12-21 08:56:55

标签: android facebook upload photo image

我正在使用官方Facebook库,我完美地发布了照片。 但我怀疑。当您第一次发布时,图像将显示在墙上方的消息中。但是当你第二次发帖时,它没有创建新帖子,上一篇文章被编辑并说“添加了2张图片”。

如果帖子位于墙的底部,并重新发布照片,我们将不会在顶部看到任何消息,将在内部消息中发布。这将是隐藏的。 你每次上传照片都可以创建一个新帖子吗?怎么可能?

我与您分享我的代码也适用于任何人都可以提供帮助。谢谢。

的OnClick:

mFacebook = new Facebook(APP_ID);
mFacebook.authorize(Principal.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
@Override
public void onComplete(Bundle values) {
    postImageonWall();
     }
 @Override
 public void onFacebookError(FacebookError error) {
     }
 @Override
 public void onError(DialogError e) {
     }
 @Override
 public void onCancel() {
     }
  });

PostWall:

public void postImageonWall() {

        byte[] data = null;

          Bitmap bi = BitmapFactory.decodeFile(fotomontatgeInSD);
          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.putByteArray("picture", data);

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


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) {
     // TODO Auto-generated method stub

 }
}

我刚刚发现新的facebook设计解决了这个问题。

2 个答案:

答案 0 :(得分:0)

尝试编辑代码,如:

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

答案 1 :(得分:0)

private void sharePhotoToFacebook(){         Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.f1);

    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(image)
            .build();

    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();

    ShareApi.share(content, null);

}