将脸书图片上传到ima​​geview

时间:2014-10-17 04:24:59

标签: java android facebook android-layout android-activity

我设法让用户将他们的照片从他们的设备库上传到ima​​geview。但是,我试图让用户能够将他们的个人资料图片从他们的Facebook上传到ima​​geview。

以下是我如何将图片从设备库上传到ima​​geview,

 Button buttonLoadImage = (Button) findViewById(R.id.btnPictureSelect);
    buttonLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

} 

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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.profilePicturePreview);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    } 

我正试图弄清楚当用户按下按钮时的方式" btnFacebookUpload"他们可以从他们的Facebook画廊中选择一张将被投射到imageview" profilePicturePreview"

的图片

更新

 Button buttonFacebookLoadImage = (Button) findViewById(R.id.btnFacebookUpload);
    buttonFacebookLoadImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            ProfilePictureView profilePictureView;
            profilePictureView = (ProfilePictureView) findViewById(R.id.profilePicturePreview);

            buttonFacebookLoadImage.setSessionStatusCallback(new Session.StatusCallback() {

                        @SuppressWarnings("deprecation")
                        @Override
                        public void call(Session session, SessionState state,
                                Exception exception) {
                Log.i(TAG, "Access Token" + session.getAccessToken());
                                Request.executeMeRequestAsync(session,
                                        new Request.GraphUserCallback() {
                                            @Override
                                            public void onCompleted(GraphUser user,
                                                    Response response) {
                                                if (user != null) {
                profilePictureView.setProfileId(user.getId());

                                                }
                                            }
                                        });
                            }



    });

问题: 不能在不同方法中定义的内部类中引用非final变量buttonFacebookLoadImage

在行

buttonFacebookLoadImage.setSessionStatusCallback(new Session.StatusCallback() {

不能在不同方法中定义的内部类中引用非final变量profilePictureView

在行

        profilePictureView.setProfileId(user.getId());

TAG无法解析为变量

1 个答案:

答案 0 :(得分:1)

使用FB Sdk You can download from here 将其作为库

添加到您的项目中

使用以下代码可以获得用户的FB个人资料图片

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.imageView);

authButton.setSessionStatusCallback(new Session.StatusCallback() {

            @SuppressWarnings("deprecation")
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
    Log.i(TAG, "Access Token" + session.getAccessToken());
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
    profilePictureView.setProfileId(user.getId());

                                    }
                                }
                            });
                }

            }