如何从我的Android应用程序发布从画廊到Facebook墙的图像?

时间:2016-12-27 12:13:24

标签: android facebook web-services sdk

我正试图从我自己的Android应用程序发布图像和文本到我的Facebook墙。我需要从移动图库中选择图像,但它不起作用。我没有得到此代码中的问题。谁能帮帮我吗??

public class PostFacebook extends Activity{
private Facebook mFacebook;
private CheckBox mFacebookCb;
private ProgressDialog mProgress;
private static final int SELECT_PICTURE = 100;
byte[] inputData;
Uri selectedImageUri;
String imgString;
ImageView image;
private Handler mRunOnUi = new Handler();

private static final String APP_ID = "7************";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.fbpost);

    final EditText reviewEdit = (EditText) findViewById(R.id.revieew);
    image = (ImageView) findViewById(R.id.imageView1);
    mFacebookCb               = (CheckBox) findViewById(R.id.cb_facebook);

    mProgress   = new ProgressDialog(this);

    mFacebook   = new Facebook(APP_ID);

    SessionStore.restore(mFacebook, this);

    if (mFacebook.isSessionValid()) {
        mFacebookCb.setChecked(true);

        String name = SessionStore.getName(this);
        name        = (name.equals("")) ? "Unknown" : name;

        mFacebookCb.setText("  Facebook  (" + name + ")");
    }

    ((Button) findViewById(R.id.button1)).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String review = reviewEdit.getText().toString();
            try {
                InputStream iStream = getContentResolver().openInputStream(selectedImageUri);
                inputData = GetImage.getBytes(iStream);
                imgString = Base64.encodeToString(inputData, Base64.DEFAULT);
                Log.v("image_check", imgString);
                Toast.makeText(PostFacebook.this, imgString, Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            if (review.equals("")) 
                return;

            if (mFacebookCb.isChecked()) postToFacebook(review, imgString);
        }
    });
    image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             Intent intent = new Intent();
             intent.setType("image/*");
             intent.setAction(Intent.ACTION_GET_CONTENT);
             startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
        }
    });
}

private void postToFacebook(String review, String image) {  
    mProgress.setMessage("Posting ...");
    mProgress.show();

    AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);

    Bundle params = new Bundle();
    params.putString("message", review);
    params.putString("picture", image);
    params.putString("name", "Hira Ghaffar");
    params.putString("caption", "Innovent.net");
    params.putString("description", "Innovent is an android app developed by iFish Technologies");
    mAsyncFbRunner.request("me/feed", params, "POST", new WallPostListener());
}

private final class WallPostListener extends BaseRequestListener {
    public void onComplete(final String response) {
        mRunOnUi.post(new Runnable() {
            @Override
            public void run() {
                mProgress.cancel();

                Toast.makeText(PostFacebook.this, "Posted to Facebook", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == RESULT_OK) {
            if (requestCode == SELECT_PICTURE) {
                 selectedImageUri = data.getData();
                if (null != selectedImageUri) { 
                image.setImageURI(selectedImageUri); 

                } 
            }
      }
}
 }

1 个答案:

答案 0 :(得分:2)

这里是Facebook墙上共享图片的完整示例。

http://simpledeveloper.com/how-to-share-an-image-on-facebook-in-android/