2
votes

I'm using Facebook 4.0 to share photos through my app but I'm unable to share photos from my phone. I can share an image from the internet by providing it's url but it doesn't work for an image from the phone. The code I'm using for sharing is below.

public void sharePhoto(Uri contentUri, String contentTitle, Bitmap image, String contentDescription){
    ShareDialog facebookDialog = new ShareDialog(getActivity());

    ShareLinkContent.Builder content = new ShareLinkContent.Builder();
    content.setContentUrl(contentUri);
    content.setContentTitle(contentTitle);
    content.setImageUrl(getImageUri(getActivity(), image));
    content.setContentDescription(contentDescription);

    ShareLinkContent shareContent = content.build();

    facebookDialog.show(shareContent);

}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

I know I can share a bitmap from the phone using the following

public void shareImage(Bitmap image){
    ShareDialog facebookDialog = new ShareDialog(getActivity());
    SharePhoto photo = new SharePhoto.Builder().setBitmap(image).build();
    SharePhotoContent content = new SharePhotoContent.Builder().addPhoto(photo).build();
    facebookDialog.show(content);

}

However this code requires that the native facebook app be installed on the phone. Is there some way to share a bitmap without having the native app installed.

2

2 Answers

3
votes

If the native app is not installed, the only other way to share a bitmap is to get publish_actions permission from the user, and do a POST to me/photos (you can use the ShareApi class to do the actual post for you, but you still need to ask for the permission from the user). Unfortunately, the web-based dialogs do not support uploading binary data.

-1
votes

Sharing multiple images with fb sdk and without native facebook installed. First login and then use below function.

private void postPhoto() {

ArrayList<SharePhoto> photos = new ArrayList<>();

    SharePhoto sharePhoto1 = new SharePhoto.Builder().setBitmap(image1).build();

    SharePhoto sharePhoto = new SharePhoto.Builder().setBitmap(image).build();

    photos.add(sharePhoto);
    photos.add(sharePhoto1);

    SharePhotoContent sharePhotoContent =
            new SharePhotoContent.Builder().setPhotos(photos).build();
    ShareApi.share(sharePhotoContent, shareCallback);
}