How to Share Image using Facebook android

前端 未结 3 1551
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 01:13

I am trying to share Image and Content via Facebook,but what i am trying is if user is not logged in via Facebook,and user click on Share button then first it should ask for

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-20 01:34

    first add 
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();    
    permissionNeeds = Arrays.asList("publish_actions");
            manager = LoginManager.getInstance();   
    **on onCreate and wirte below code in send button lick event**
     send.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
    
                            if (isOnline()) {
                                new WebView(MainActivity.this);
    
                                manager.logInWithPublishPermissions(MainActivity.this, permissionNeeds);
                                manager.registerCallback(callbackManager, new FacebookCallback() {
    
                                    @Override
                                    public void onSuccess(LoginResult loginResult) {
                                        sharePhotoToFacebook();
                                    }
    
                                    @Override
                                    public void onCancel() {
                                        System.out.println("onCancel");
                                    }
    
                                    @Override
                                    public void onError(FacebookException exception) {
    
                                        exception.printStackTrace();
                                    }
                                });
    
    
                                alertDialog.dismiss();
                            } else {
                                Toast.makeText(getBaseContext(), "Please check internet connection", Toast.LENGTH_SHORT).show();
                            }
    
                        }
                    });
    
    
         private void sharePhotoToFacebook() {
                try {
                    Log.i("Facebook Image", camera_pathname + "");
                    Bitmap image = BitmapFactory.decodeFile(camera_pathname);
                    //Bitmap image = decodeFile(camera_pathname);
                    image = getResizedBitmap(image);
                    //Bitmap image = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    
                    SharePhoto photo = new SharePhoto.Builder()
                            .setBitmap(image)
                            .setCaption(et_hashtag.getText().toString())
                            .build();
    
                    SharePhotoContent content = new SharePhotoContent.Builder()
                            .addPhoto(photo)
                            .build();
    
                    ShareApi.share(content, null);
                    Toast.makeText(getBaseContext(), "Shared successfully", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
    
                // manager.logOut();
            }
    

提交回复
热议问题