从Android应用程序发布照片到Facebook,照片来自Android Gallery

时间:2012-11-28 08:16:08

标签: android facebook post image

我目前正在开发Android应用程序目标版本是4.0 Ice-Cream Sandwich。

到目前为止,我可以使用以下代码将正常文本发布到Facebook上:

public void postToWall() {

      // post on user's wall.

        facebook.dialog(this, "feed", new DialogListener() {

    @Override
    public void onFacebookError(FacebookError e) {
    }

    @Override
    public void onError(DialogError e) {
    }

    @Override
    public void onComplete(Bundle values) {
    }

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

}

但是,我无法将照片发布到带有字幕的Facebook上。我在网上搜索,其中一个代码是:

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

        Bitmap bi = BitmapFactory.decodeFile(photoToPost);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();

        Bundle params = new Bundle();
        params.putString("method", "photos.upload");
        params.putByteArray("picture", data);

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

    }

问题是,上面的代码不起作用,因为我不知道什么:

1 photoToPost

2. mAsyncRunner .request一直给我一个错误,指出我不能把“null”,因为它是无效的争论

3. SampleUploadListener ,据说是来自FacebookSDK不能正常工作(我不断收到错误来创建一个类)

这里有更简单的代码吗?或者有人可以向我解释我遇到的错误。

到目前为止,我正在使用“On Click”将正常文本发布到Facebook上,它指向此方法。我的目标是将带有标题的照片上传到Facebook。

谢谢大家的帮助!

5 个答案:

答案 0 :(得分:0)

link。这将向您展示如何在FaceBook墙上发布图像,如何在墙上发布文字。

很有学问。

答案 1 :(得分:0)

1 - 这是您的照片将被发送到墙上,它可以是您的SD卡或其他任何地方的图像

2 - 这是来自Facebook SDK的一个类,它接受facebook对象(之前创建的一个)

3 - 再次来自Facebook SDK的课程

您的Facebook SDK似乎有问题 尝试使用右键单击项目>>再次设置它属性>> Android并查看是否存在库

答案 2 :(得分:0)

问题在于以下一行

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

您将null作为图形路径传递,这应该像this =“me / feed”

<强>更新  写下这行代码

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

然后它应该工作。

答案 3 :(得分:0)

public class CardShared extends Activity{

public static final String APP_ID = "YOUR APP ID";
private Facebook mFacebook;
private AsyncFacebookRunner mAsyncRunner ;
boolean isLoggedIn = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   // setContentView(R.layout.main);
    mFacebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(mFacebook);

    //Implementing SSO
    mFacebook.authorize(this, new String[]{"publish_stream"}, new DialogListener(){

        public void onComplete(Bundle values) {

            sharePicture(values.getString(Facebook.TOKEN));
            Toast.makeText(getApplicationContext(), "Picture Shared Successfully", Toast.LENGTH_SHORT).show();
            CardShared.this.finish();
        }


        public void onFacebookError(FacebookError e) {
            Log.d("FACEBOOK ERROR","FB ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
        }


        public void onError(DialogError e) {
            Log.e("ERROR","AUTH ERROR. MSG: "+e.getMessage()+", CAUSE: "+e.getCause());
        }


        public void onCancel() {
            Log.d("CANCELLED","AUTH CANCELLED");
        }
    });
}

//updating Status
public void sharePicture(String accessToken){

    byte[] data = null;
    try {
        Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.image_to_be_uploaded);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        data = baos.toByteArray();

        Bundle params = new Bundle();
        params.putString("method", "photos.upload");
        params
                .putString(Facebook.TOKEN, mFacebook
                        .getAccessToken());
        params.putByteArray("picture", data);
        mAsyncRunner.request(null, params, "POST",
                new SampleUploadListener(), null);

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

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.d("onActivityResult","onActivityResult");
    mFacebook.authorizeCallback(requestCode, resultCode, data);
}
public class SampleUploadListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        try {

            Log.d("Facebook-Example", "Response: " + response.toString());
            JSONObject json = Util.parseJson(response);
            final String f = json.getString("src");

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

}

答案 4 :(得分:0)

就像这样

mAsyncRunner.request("me/feed", params, "POST", new SampleUploadListener())