I want to share an image from the device to facebook. I have searched over the internet and found several examples but all of them weren't using the latest facebook sdk. I followed the doc on the facebook developer's page.
Here is my code:
private static final int SELECT_FILE = 999;
private UiLifecycleHelper uiHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_screen);
ButterKnife.inject(this);
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@OnClick(R.id.button_fb)
public void onButtonClicked(View v) {
if (v.getId() == R.id.button_fb) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media
.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}
}
public void postImageOnFacebook(Bitmap bitmap) {
List<Bitmap> bitmapList = new ArrayList<>();
bitmapList.add(bitmap);
if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
FacebookDialog.ShareDialogFeature.PHOTOS)) {
// Publish the post using the Photo Share Dialog
Utils.showToast(activity, "Calling");
FacebookDialog shareDialog = new FacebookDialog.PhotoShareDialogBuilder(this)
.addPhotos(bitmapList)
.setPlace(AppConstants.HASH_TAG)
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
Utils.showToast(activity, "Facebook App for android was not found in this device");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && data != null) {
try {
Bitmap bitmap;
Uri selectedImageUri = data.getData();
String tempPath = getPath(selectedImageUri, activity);
BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
compressBitmap = getResizeBitmap(bitmap, 1024);
return true;
} catch (Exception e) {
Timber.e(Log.getStackTraceString(e));
return false;
}
}
uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
@Override
public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
Timber.e(String.format("Error: %s", error.toString()));
}
@Override
public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
Timber.i("Success!");
}
});
}
public String getPath(Uri uri, Activity activity) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor cursor = activity
.managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public Bitmap getResizeBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 0) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
}
The code works perfect; but whenever I select any image the image upload is initiated BUT within a second an error dialog pops up saying "FACEBOOK UPLOAD FAILED, Your photo couldn't be uploaded" The logcat is clean and shows no error msg.
Also I need to know how can I post image on the Wall Post [Not in the Gallery] with #HASHTAG that I provide.
Please help!
Thanks in advance.