其他分享
首页 > 其他分享> > 如何使用Facebook android分享图像

如何使用Facebook android分享图像

作者:互联网

我试图通过Facebook分享图像和内容,但我想要的是如果用户没有通过Facebook登录,并且用户点击“共享”按钮然后首先应该要求登录,但是当我运行我的代码时,我没有得到错误,但没有任何事情发生在屏幕上,它进入我的其他部分和在logcat它显示其他部分的消息

适配器

 holder.sharefb.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (strtextfb == null) {


                        share();
                    }
                }
            });

方法

 public void RequestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object,GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if(json != null){
                        String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link");
                       // details_txt.setText(Html.fromHtml(text));
                       // profile.setProfileId(json.getString("id"));


                       // System.out.println("FbId" + fbids);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,email,picture");
        request.setParameters(parameters);
        request.executeAsync();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);


    }

    public void share() {
         if (AccessToken.getCurrentAccessToken() != null) {
        RequestData();
        String pictureUrl = "https://upload.wikimedia.org/wikipedia/en/9/90/Bale_as_Batman.jpg";
        Bundle params = new Bundle();
        params.putString("name", "" + "Aditya");
        params.putString("message", "" + "Nver Give Up");
        params.putString("caption", "" + "Hi");
        params.putString("description", "" + "Lie Cheat Steal");
        params.putString("link", "" + "https://www.google.co.in/");
        if (pictureUrl != "")
            params.putString("picture", "" + pictureUrl);
    /* make the API call */
        new GraphRequest(
                AccessToken.getCurrentAccessToken(),
                "/me/feed",
                params,
                HttpMethod.POST,
                new GraphRequest.Callback() {
                    public void onCompleted(GraphResponse response) {
                    /* handle the result */
                        Log.e("res", "" + response.getError());
                    }
                }
        ).executeAsync();
    }
    else
    {
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                    @Override
                    public void onSuccess(LoginResult loginResult) {

                        GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) {

                            }
                        });

                    }

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void one rror(FacebookException e) {

                    }
                });
                System.out.println("else part");

    }
}

解决方法:

您需要在适配器上单击按钮时调用以下代码.

List<String> permissionNeeds = Arrays.asList("publish_actions");

		manager = LoginManager.getInstance();

		manager.logInWithPublishPermissions(this, permissionNeeds);

		manager.registerCallback(callbackManager,
				new FacebookCallback<LoginResult>() {
					@Override
					public void onSuccess(LoginResult loginResult) {
						publishImage();

					}

					@Override
					public void onCancel() {
						System.out.println("onCancel");
					}

					@Override
					public void one rror(FacebookException exception) {
						System.out.println("onError");
					}
				});

	}


    private void publishImage() {
		Bitmap image = BitmapFactory.decodeResource(getResources(),
				R.drawable.ic_launcher);

    //You need to get bitmap from any source.

		SharePhoto photo = new SharePhoto.Builder().setBitmap(image)
				.setCaption("Welcome To Facebook Photo Sharing on steroids!")
				.build();

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

		ShareApi.share(content, null);
		Toast.makeText(this, "Succsesfully posted on your wall",
				Toast.LENGTH_LONG).show();

	}

标签:android,facebook,facebook-share,baseadapter
来源: https://codeday.me/bug/20191002/1842291.html