如何在android中的脸书上分享图像

时间:2014-04-07 05:16:23

标签: android

以下是我能够在Share i want share image上以文本格式共享数据的代码。

假设我在SD卡上有图像,请帮我如何在脸书上分享图像。

我该怎么做?

这是我的代码:

btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                String title = "This is facebook Data";

                try {
                    Intent i = new Intent(Intent.ACTION_VIEW);

                    i.setData(Uri.parse("https://m.facebook.com/sharer.php?u="
                            + "www.facebook.com" + "&t=" + title + "&_rdr"));

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

2 个答案:

答案 0 :(得分:0)

请试试这个:

 private void fbImageSubmit(Facebook fb, String imageurl, String caption, String description, String name, String linkurl)
    {
        if(fb != null)
        {
            if(fb.isSessionValid())
            {
                Bundle b = new Bundle();
                b.putString("picture", imageurl);
                b.putString("caption",caption);
                b.putString("description",description );
                b.putString("name",name);
                b.putString("link",linkurl);
                try {
                    String strRet = "";
                    strRet = fb.request("/me/feed",b,"POST");
                    JSONObject json;
                    try {
                        json = Util.parseJson(strRet);
                        if(!json.isNull("id"))
                        {
                            Log.i("Facebook", "Image link submitted.");
                        }
                        else
                        {
                            Log.e("Facebook","Error: " + strRet);
                        }
                    } catch (FacebookError e) {
                        Log.e("Facebook","Error: " + e.getMessage());
                    }
                } catch (Exception e) {
                    Log.e("Facebook", "Error: " + e.getMessage());
                }
            }
        }
    }

答案 1 :(得分:0)

试试这个..

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

    }
}

通过这些链接也可以:

最简单的方法是使用现有的SDK,类似:

http://github.com/facebook/facebook-android-sdk/

http://code.google.com/p/fbconnect-android/

http://wiki.developers.facebook.com/index.php/User:Android

更灵活的方法是自己实施API,以下是有用的文档:

http://developers.facebook.com/docs/

相关问题