title: Android Sharing on Facebook (SDK 4)
body: <p>Trying to share an image with Facebook SDK 4.0 for android.</p>
  
  <p>app.graddle</p>
  
  <pre><code>dependencies {
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.android.support:appcompat-v7:22.1.1'
      // Facebook's library to implement Login with Facebook
      compile 'com.facebook.android:facebook-android-sdk:4.0.1'
  }
  </code></pre>
  
  <p>AndroidManifest.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.facebookshare" &gt;
      &lt;uses-permission android:name="android.permission.INTERNET" /&gt;    
      &lt;application
          android:allowBackup="true"
          android:icon="@mipmap/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
  
          &lt;meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/&gt;
  
          &lt;activity
              android:name=".MainActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
  
          &lt;activity android:name="com.facebook.FacebookActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
              android:label="@string/app_name" /&gt;
  
          &lt;provider android:authorities="com.facebook.app.FacebookContentProvider1234"
              android:name="com.facebook.FacebookContentProvider"
              android:exported="true"/&gt;
  
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>MainActivity.java</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements View.OnClickListener {
      private static final int ACTION_PICK_IMAGE = 1;
  
      //Facebook client
      private CallbackManager fb_callbackManager;
      private ShareDialog fb_shareDialog;
  
      private Button button;
      private ImageView imageView;
      private EditText editText;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          // Initialize Facebook API
          FacebookSdk.sdkInitialize(getApplicationContext());
          fb_callbackManager = CallbackManager.Factory.create();
  
          setContentView(R.layout.activity_main);
  
          imageView = (ImageView) findViewById(R.id.imageView);
          imageView.setOnClickListener(this);
  
          editText = (EditText) findViewById(R.id.editText);
  
          button = (Button) findViewById(R.id.button);
          button.setOnClickListener(this);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();
  
          //noinspection SimplifiableIfStatement
          if (id == R.id.action_settings) {
              return true;
          }
  
          return super.onOptionsItemSelected(item);
      }
  
      @Override
      public void onClick(View v) {
          switch (v.getId()){
              case R.id.imageView:
                  loadImage();
                  break;
              case R.id.button:
                  share();
                  break;
          }
      }
  
      private void loadImage(){
          Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
          photoPickerIntent.setType("image/*");
          photoPickerIntent.putExtra("crop", "true");
          photoPickerIntent.putExtra("scale", true);
          photoPickerIntent.putExtra("scaleUpIfNeeded", true);
          photoPickerIntent.putExtra("max-width", 170);
          photoPickerIntent.putExtra("max-height", 160);
          photoPickerIntent.putExtra("aspectX", 1);
          photoPickerIntent.putExtra("aspectY",1);
  
          startActivityForResult(photoPickerIntent, ACTION_PICK_IMAGE);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(requestCode == ACTION_PICK_IMAGE) {
              if (resultCode == RESULT_OK) {
                  Bundle extras = data.getExtras();
                  //get the cropped bitmap
                  Bitmap thePic = extras.getParcelable("data");
                  imageView.setImageBitmap(thePic);
              }
          }
      }
  
      protected void share(){
          String shareText = editText.getText().toString();
          ShareDialog shareDialog = new ShareDialog(this);
          if (ShareDialog.canShow(SharePhotoContent.class)) {
              shareDialog.registerCallback(fb_callbackManager, new FacebookCallback&lt;Sharer.Result&gt;() {
                  @Override
                  public void onSuccess(Sharer.Result result) {
                      Toast.makeText(MainActivity.this, "Share Success", Toast.LENGTH_SHORT).show();
                      Log.d("DEBUG", "SHARE SUCCESS");
                  }
  
                  @Override
                  public void onCancel() {
                      Toast.makeText(MainActivity.this, "Share Cancelled", Toast.LENGTH_SHORT).show();
                      Log.d("DEBUG", "SHARE CACELLED");
                  }
  
                  @Override
                  public void onError(FacebookException exception) {
                      Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
                      Log.e("DEBUG", "Share: " + exception.getMessage());
                      exception.printStackTrace();
                  }
              });
  
              SharePhoto photo = new SharePhoto.Builder()
                      .setBitmap(((BitmapDrawable) imageView.getDrawable()).getBitmap())
                      .build();
              SharePhotoContent content = new SharePhotoContent.Builder()
                      .addPhoto(photo)
                      .build();
  
              shareDialog.show(content);
          }
  
      }
  }
  </code></pre>
  
  <p>Nothing is shown, or at least not visible, because I have to hit back to be able to interact with my activity again after trying to share on Facebook.</p>
  
  <p>No DEBUG string is logged on Logcat. So I understand ShareDialog can show Photo content.</p>
  
  <p>Could somebody post a simple working example of sharing image with Facebook for SDK 4.0(android)?</p>