0

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.

AabidMulani
  • 2,325
  • 1
  • 28
  • 47
  • Can you see the photo previews when the share dialog is shown? Have you also added a ContentProvider to the manifest as described in the javadocs for addPhotos? – Ming Li Jan 02 '15 at 18:55
  • Yes, i can see the photo preview. Also the posting is initiated by facebook. But with in a second i get a notification from facebook app saying the "Upload has Failed" – AabidMulani Jan 03 '15 at 08:52
  • Wait, what is your setPlace(AppConstants.HASH_TAG)? – Ming Li Jan 05 '15 at 20:13
  • I want to add a hashTag with the image, so i just tried to add the HashTag in the place field – AabidMulani Jan 06 '15 at 08:15
  • 1
    You can't just add random values for parameters that weren't designed to take those values. This is why your share is failing. There's no hashtag support via API, and in any case, pre-filling the message for the user is against platform policy, so you can't do it. – Ming Li Jan 06 '15 at 18:25

0 Answers0