Mahendran Candy
Mahendran Candy

Reputation: 1144

How to upload image to facebook from sdcard?

Here i placed my code can anyone tell what went wrong?

CODE

private void onFbLogin() {
            LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_friends"));
            LoginManager.getInstance().registerCallback(callbackManager,
                    new FacebookCallback<LoginResult>() {
                        @Override
                        public void onSuccess(LoginResult loginResult) {
                            System.out.println("Success");
                            if (ShareDialog.canShow(ShareLinkContent.class)) {                          
                                 File sdCard = Environment.getExternalStorageDirectory();
                               File file = new File(sdCard, "final_card.jpg");



                            ShareLinkContent content = new ShareLinkContent.Builder()//
                                    .setContentUrl(Uri.parse("file:///sdcard/sharing_event.jpg"))
                                    .setContentDescription("some desc")
                                    .setContentTitle("Chennai")
                                    .build();
                            shareDialog.show(content);
                        }
                    }

                    @Override
                    public void onCancel() {
                        showAlert();
                    }

                    @Override
                    public void onError(FacebookException error) {
                        showAlert();
                    }

                    private void showAlert() {
                        new AlertDialog.Builder(FinalCardView.this)
                                .setTitle(getString(R.string.cancelled))
                                .setMessage(getString(R.string.permission_not_granted))
                                .setPositiveButton(getString(R.string.ok), null)
                                .show();
                    }
                });
    }

Upvotes: 2

Views: 203

Answers (2)

Mahendran Candy
Mahendran Candy

Reputation: 1144

finally its works for me... without any login and publish_action permissions... but native android app installed in our mobile...

public void shareFacebookUsingGraph(){

    ShareOpenGraphObject object = new ShareOpenGraphObject.Builder()
    .putString("og:type", "books.book")
    .putString("og:title", "A Game of Thrones")
    .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.")
    .putString("books:isbn", "0-553-57340-3")
    .build();

    // Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.message_box);

     File sd = Environment.getExternalStorageDirectory();
        File image_file = new File(sd, "sharing_event.jpg");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap_image = BitmapFactory.decodeFile(image_file.getAbsolutePath(), options);

    SharePhoto photo = new SharePhoto.Builder()
    .setBitmap(bitmap_image)
    .setUserGenerated(true)
    .build();

// Create an action
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder()
    .setActionType("books.reads")
    .putObject("book", object)
    .putPhoto("image", photo)
    .build();

ShareOpenGraphContent content = new ShareOpenGraphContent.Builder()
.setPreviewPropertyName("book")
.setAction(action)
.build();

ShareDialog.show(FinalCardView.this, content);
}

Upvotes: 1

PeDuCKA
PeDuCKA

Reputation: 553

Do you want to share the picture - it needs to use SharePhoto photo

SharePhoto photo = new SharePhoto.Builder()
        .setBitmap(bitmap)
        .build();
SharePhotoContent content = new SharePhotoContent.Builder()
        .addPhoto(photo)
        .build();

So you can get a bitmap file

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()

Upvotes: 1

Related Questions