Question: How to post images selected from gallery to Facebook wall from my android app?

Question

How to post images selected from gallery to Facebook wall from my android app?

Answers 1
Added at 2016-12-27 13:12
Tags
Question

I am trying to post image and text to my facebook wall from my own android app. I need to select the image from mobile gallery, but its not working. i am not getting what is the issue in this code. Can anyone please help me??

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

                } 
            }
      }
}
 }
Answers
nr: #1 dodano: 2016-12-27 13:12

Here, The complete example of share image on facebook wall.

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

Source Show
◀ Wstecz