Android的发布图片至Facebook [英] Android Post Image to Facebook

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

问题描述

我在我的应用程序这个code运行,它正试图发布的文字和图像,以一个用户的墙。目前它只是张贴的文字。我想我错过了一些东西简单,但将AP preciate另一双眼睛的检查一切或其他样本。

任何帮助是极大AP preciated。

 捆绑包=新包();
            bundle.putString(消息,测试更新); //消息告诉你更新你的Facebook状态
            bundle.putString(Facebook.TOKEN,的accessToken);
            bundle.putString(附件,{\\名称\\:\\我的测试图像\\,
+\\HREF \\:\\+http://www.google.com+\\,
+\"\\\"media\\\":[{\\\"type\\\":\\\"image\\\",\\\"src\\\":\\\"\"+\"http://www.google.com/logos/mucha10-hp.jpg\"+\"\\\",\\\"href\\\":\\\"\"+\"http://www.google.com\"+\"\\\"}]\"
+});
                    +});
            //告诉你正在执行的身份验证的用户墙这个动作脸谱,从而
//它成为一个更新。 POST告诉正在使用的方法是POST
            字符串的响应= facebook.request(ME /饲料,捆绑,POST);


解决方案

希望这会为你工作。

对于Facebook varible工具创建类

进口android.app.Application;

 进口com.facebook.android.AsyncFacebookRunner;
进口com.facebook.android.Facebook;公共类工具扩展应用{
    公共静态的Facebook mFacebook;
    公共静态AsyncFacebookRunner mAsyncRunner;
    公共静态字符串userUID;
    公共静态最后弦乐ICON_URL =htt​​p://i.imgur.com/6G1b7.png;}

现在马托用于张贴图片的Facebook墙上

 公共无效postOnFacebookPicture(最终位图位图){        字符串的access_token = M prefs.getString(的access_token,NULL);
        长到期= M prefs.getLong(access_expires,0);        如果(ACCESS_TOKEN!= NULL){
            Utility.mFacebook.setAccessToken(的access_token);        }
        如果(到期!= 0){
            Utility.mFacebook.setAccessExpires(到期);
        }        如果(!Utility.mFacebook.isSessionValid()){
            showErrorDialog(
                    Facebook的帐户没有设置,设置的Facebook帐户?
                    新DialogInterface.OnClickListener(){
                        公共无效的onClick(DialogInterface对话,诠释的id){
                            尝试{
                                 //移动到设定的Facebook帐户
                            }赶上(例外五){
                                // TODO自动生成catch块
                                e.printStackTrace();
                            }                        }
                    },新DialogInterface.OnClickListener(){
                        公共无效的onClick(DialogInterface对话,诠释的id){
                            dialog.cancel();                        }
                    });
        }其他{
            新的Thread(){
                @覆盖
                公共无效的run(){
                    诠释什么= 0;                    尝试{                        字符串的accessToken = M prefs.getString(ACCESS_TOKEN
                                空值);                        ByteArrayOutputStream BOS =新ByteArrayOutputStream();
                        bitmap.com preSS(比较pressFormat.PNG,0,BOS);
                        字节[] = pictureData bos.toByteArray();                        束束=新包();
                        bundle.putByteArray(facebookPictureData,pictureData);
                        bundle.putString(Facebook.TOKEN,的accessToken);                        Utility.mFacebook.request(ME /照片,捆绑,POST);                    }赶上(例外五){
                        什么= 1;
                    }                    mHandler.sendMessage(mHandler.obtainMessage(什么));
                }
            }。开始();
        }    }

I have this code running in my app and it is trying to post text and an image to a users wall. At the moment it is only posting the text. I think I have missed something simple, but would appreciate another pair of eyes check everything or another sample.

Any help is greatly appreciated.

            Bundle bundle = new Bundle();
            bundle.putString("message", "test update"); //'message' tells facebook that you're updating your status
            bundle.putString(Facebook.TOKEN,accessToken);
            bundle.putString("attachment", "{\"name\":\"My Test Image\","
+"\"href\":\""+"http://www.google.com"+"\","
+"\"media\":[{\"type\":\"image\",\"src\":\""+"http://www.google.com/logos/mucha10-hp.jpg"+"\",\"href\":\""+"http://www.google.com"+"\"}]"
+"}");
                    +"}");
            //tells facebook that you're performing this action on the authenticated users wall, thus 
//          it becomes an update. POST tells that the method being used is POST
            String response = facebook.request("me/feed",bundle,"POST");

解决方案

Hope this will be work for you

Create class for facebook varible utility

import android.app.Application;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.Facebook;

public class Utility extends Application{
    public static Facebook mFacebook;
    public static AsyncFacebookRunner mAsyncRunner;
    public static String userUID;
    public static final String ICON_URL = "http://i.imgur.com/6G1b7.png";

}

Now mathod used to post image to facebook wall

public void postOnFacebookPicture(final Bitmap bitmap) {

        String access_token = mPrefs.getString("access_token", null);
        long expires = mPrefs.getLong("access_expires", 0);

        if (access_token != null) {
            Utility.mFacebook.setAccessToken(access_token);

        }
        if (expires != 0) {
            Utility.mFacebook.setAccessExpires(expires);
        }

        if (!Utility.mFacebook.isSessionValid()) {
            showErrorDialog(
                    "Facebook Account is not configure,Setting Facebook Account?",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            try {
                                 // Move to setting the facebook account
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }

                        }
                    }, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.cancel();

                        }
                    });
        } else {
            new Thread() {
                @Override
                public void run() {
                    int what = 0;

                    try {

                        String accessToken = mPrefs.getString("access_token",
                                null);

                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        bitmap.compress(CompressFormat.PNG, 0, bos);
                        byte[] pictureData = bos.toByteArray();

                        Bundle bundle = new Bundle();
                        bundle.putByteArray("facebookPictureData", pictureData);
                        bundle.putString(Facebook.TOKEN, accessToken);

                        Utility.mFacebook.request("me/photos", bundle, "POST");

                    } catch (Exception e) {
                        what = 1;
                    }

                    mHandler.sendMessage(mHandler.obtainMessage(what));
                }
            }.start();
        }

    }

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

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