Trying to share an image with Facebook SDK 4.0 for android.
app.graddle
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'
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.facebookshare" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<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" />
<provider android:authorities="com.facebook.app.FacebookContentProvider1234"
android:name="com.facebook.FacebookContentProvider"
android:exported="true"/>
</application>
</manifest>
MainActivity.java
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<Sharer.Result>() {
@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);
}
}
}
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.
No DEBUG string is logged on Logcat. So I understand ShareDialog can show Photo content.
Could somebody post a simple working example of sharing image with Facebook for SDK 4.0(android)?