安卓将图片发布到脸书墙

2022-09-02 09:07:03

我正在尝试在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());
    }
}

答案 1

第一件事是你没有使用图形API上传图片...您正在使用旧的休息 api...尝试使用图形api,它很简单...

使用以下代码:

Bundle param = new Bundle();
param.putString("message", "picture caption");
param.putByteArray("picture", ImageBytes);
mAsyncRunner.request("me/photos", param, "POST", new SampleUploadListener());

根据错误消息,它看起来像是在从意图的捆绑包中获取字节时给出错误...


答案 2
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() {
                    }
                });

            }
        });

public void postImageonWall() {

           byte[] data = null;

             Bitmap bi = BitmapFactory.decodeFile("/sdcard/viewitems.png");
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
             bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
             data = baos.toByteArray();


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

             AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(facebook);
             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) {    
    }
}

试试这个代码,它会工作,我使用了相同的代码,并将图像上传到Facebook上。