在 Android 中使用 Facebook Android SDK 将照片上传到 Facebook

发布于 2024-09-07 05:09:15 字数 79 浏览 7 评论 0原文

我是安卓新手。我正在搜索通过授权将照片加载到 Facebook,获取 access_token。如何做到这一点?请给我一个 Java 示例代码。

I'm new to Android. I'm searching for load photo to facebook by authorization, getting access_token. How to do this? Please give me a sample code in Java.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倒数 2024-09-14 05:09:15

刚刚在这里发布了上传照片的简单方法:

android facebook 发布照片

代码:

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

Just posted here the simple way to upload a photo:

android facebook publish photo

Code:

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);
柠檬色的秋千 2024-09-14 05:09:15
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) {
        // 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) {
        // TODO Auto-generated method stub

    }
}

here is the whole code try it it will definately work for you and it is work for me also

も让我眼熟你 2024-09-14 05:09:15

对您来说最简单的方法是使用现有的 SDK,如下所示:
http://github.com/facebook/facebook-android-sdk/< br>
http://code.google.com/p/fbconnect-android/< br>
http://wiki.developers.facebook.com/index.php/User: Android

更灵活的方法是自己实现 API,以下是有用的文档:
http://developers.facebook.com/docs/

The easiest way for you is to use the existing SDK, something like that:
http://github.com/facebook/facebook-android-sdk/
http://code.google.com/p/fbconnect-android/
http://wiki.developers.facebook.com/index.php/User:Android

The more flexible way is to implement the API yourself, here are the docs that will be useful:
http://developers.facebook.com/docs/

时光倒影 2024-09-14 05:09:15

如果您想发布带有描述的照片,您可以执行以下操作:

public void publishPhoto(byte[] imgData, string message) {

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner (facebook);

Bundle parameters = new Bundle ();
parameters.putString (Facebook.TOKEN, facebook.getAccessToken());
parameters.putString ("message", message);
parameters.putByteArray ("source", imgData);

mAsyncRunner.Request ("me/photos", parameters, "POST", new RequestListener (), null);
}

If you want to publish a photo with a description you can do this :

public void publishPhoto(byte[] imgData, string message) {

AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner (facebook);

Bundle parameters = new Bundle ();
parameters.putString (Facebook.TOKEN, facebook.getAccessToken());
parameters.putString ("message", message);
parameters.putByteArray ("source", imgData);

mAsyncRunner.Request ("me/photos", parameters, "POST", new RequestListener (), null);
}
亽野灬性zι浪 2024-09-14 05:09:15

我已成功将照片上传到 Facebook 墙上
我使用了以下代码。

在您的主要活动中执行以下操作;

btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            authenticatedFacebook.authorize(Facebooktest2Activity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                        postImage();
                        Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show();


                    }

                    @Override
                    public void onFacebookError(FacebookError error) {
                    }

                    @Override
                    public void onError(DialogError e) {
                    }

                    @Override
                    public void onCancel() {
                    }

    });


        }
});






}

public void postImage(){
     byte[] data = null;               

        Bitmap bi = BitmapFactory.decodeFile("/sdcard/img.jpg");
        //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);             
        ByteArrayOutputStream baos = new ByteArrayOutputStream();              
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);              
        data = baos.toByteArray();                
        Bundle params = new Bundle();              
        params.putString(Facebook.TOKEN, authenticatedFacebook.getAccessToken());              
        params.putString("method", "photos.upload");              
        params.putByteArray("picture", data);               
        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(authenticatedFacebook);              
        mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);   

}


public class SampleUploadListener extends BaseKeyListener implements RequestListener {

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

    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

    public Bitmap getInputType(Bitmap img) {
        // TODO Auto-generated method stub
        return img;
    }

    @Override
    public int getInputType() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }
}

此代码当然会对您有所帮助。

Successfully i have uploaded the photos on facebook wall
I used the following code.

In your Main Activity do the following;

btn1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            authenticatedFacebook.authorize(Facebooktest2Activity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
                    @Override
                    public void onComplete(Bundle values) {
                        postImage();
                        Toast.makeText(getApplicationContext(), "Image Posted on Facebook.", Toast.LENGTH_SHORT).show();


                    }

                    @Override
                    public void onFacebookError(FacebookError error) {
                    }

                    @Override
                    public void onError(DialogError e) {
                    }

                    @Override
                    public void onCancel() {
                    }

    });


        }
});






}

public void postImage(){
     byte[] data = null;               

        Bitmap bi = BitmapFactory.decodeFile("/sdcard/img.jpg");
        //Bitmap bi = BitmapFactory.decodeResource(getResources(), R.drawable.icon);             
        ByteArrayOutputStream baos = new ByteArrayOutputStream();              
        bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);              
        data = baos.toByteArray();                
        Bundle params = new Bundle();              
        params.putString(Facebook.TOKEN, authenticatedFacebook.getAccessToken());              
        params.putString("method", "photos.upload");              
        params.putByteArray("picture", data);               
        AsyncFacebookRunner mAsyncRunner = new AsyncFacebookRunner(authenticatedFacebook);              
        mAsyncRunner.request(null, params, "POST", new SampleUploadListener(), null);   

}


public class SampleUploadListener extends BaseKeyListener implements RequestListener {

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

    public void onFacebookError(FacebookError e, Object state) {
        // TODO Auto-generated method stub

    }

    public Bitmap getInputType(Bitmap img) {
        // TODO Auto-generated method stub
        return img;
    }

    @Override
    public int getInputType() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void onIOException(IOException e, Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onFileNotFoundException(FileNotFoundException e,
            Object state) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onMalformedURLException(MalformedURLException e,
            Object state) {
        // TODO Auto-generated method stub

    }
}

Surely This code will help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文