Android发布图片到Facebook墙 [英] Android post picture to Facebook wall

查看:105
本文介绍了Android发布图片到Facebook墙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在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,这意味着字节数组不为空



NB。 Logcat输出一些警告(不是错误):

  03-02 14:19:29.554:WARN / Bundle(1891)抛出生成的内部异常:
03-02 14:19:29.554:WARN / Bundle(1891):java.lang.ClassCastException:java.lang.String
03-02 14:19:29.554: WARN / Bundle(1891):在android.os.Bundle.getByteArray(Bundle.java:1305)
03-02 14:19:29.554:WARN / Bundle(1891):在com.facebook.android.Util .openUrl(Util.java:155)
03-02 14:19:29.554:WARN / Bundle(1891):在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 / (1891):键方法预期的byte [],但值是一个java.lang.String。默认值< null>被归还

(在彼此之下显示多次)



我做错了什么?






SOLVED。 >
这是我做的使它工作:

  facebook.authorize(this,new String [] { publish_stream},
new DialogListener(){

@Override
public void onFacebookError(FacebookError e){
// TODO自动生成的方法stub


@Override
public void onError(DialogError dialogError){
// TODO自动生成的方法存根
}

@Override
public void onComplete(Bundle values){
postToWall(values.getString(Facebook.TOKEN));
}

@Override
public void onCancel(){
// TODO自动生成的方法存根
}
});

助手方法:

  private void postToWall(String accessToken){
Bundle params = new Bundle();

params.putString(Facebook.TOKEN,accessToken);

//字节数组是图片的数据。
params.putByteArray(picture,getIntent()。getExtras()。getByteArray(data));

尝试{
facebook.request(我/照片,参数,POST);

} catch(FileNotFoundException fileNotFoundException){
makeToast(fileNotFoundException.getMessage());
} catch(MalformedURLException malformedURLException){
makeToast(malformedURLException.getMessage());
} catch(IOException ioException){
makeToast(ioException.getMessage());
}
}


解决方案

事情是你没有使用图形API来上传图片...使用旧的休息api ...尝试使用图形API,其简单的...



使用以下代码:

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

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


I am trying to post a picture to my wall on Facebook. I have managed logging in and posting text to the wall. However, when I try posting the picture, nothing happens.

I am using the Android Facebook SDK.

Here is what I have so far:

    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);

The Toast shows 8733, which means the byte array isn't empty

NB. Logcat output some warnings (not errors):

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.

(Shows several times underneath each other.)

What am I doing wrong?


SOLVED. This is what I did to make it work:

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
    }
});

And the helper method:

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());
    }
}

解决方案

first thing is that you are not using graph api to upload the pictures... u r using the old rest api... try to use graph api, its simple...

Use following code:

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

According to error message, it looks like its giving errors in getting bytes from intent's bundle...

这篇关于Android发布图片到Facebook墙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆