无法使用FacebookDialog.PhotoShareDialogBu​​ilder共享图像

时间:2015-01-01 20:32:19

标签: android facebook facebook-graph-api facebook-android-sdk

我想将设备中的图像分享到Facebook。我通过互联网搜索并找到了几个例子,但他们都没有使用最新的facebook sdk。我在facebook开发者的页面上关注了文档。

这是我的代码:

        private static final int SELECT_FILE = 999;
    private UiLifecycleHelper uiHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_screen);
        ButterKnife.inject(this);
        uiHelper = new UiLifecycleHelper(this, null);
        uiHelper.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        uiHelper.onResume();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }



    @OnClick(R.id.button_fb)
    public void onButtonClicked(View v) {
        if (v.getId() == R.id.button_fb) {
            Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media
                    .EXTERNAL_CONTENT_URI);
            intent.setType("image/*");
            startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
        }
    }


    public void postImageOnFacebook(Bitmap bitmap) {
        List<Bitmap> bitmapList = new ArrayList<>();
        bitmapList.add(bitmap);
        if (FacebookDialog.canPresentShareDialog(getApplicationContext(),
                FacebookDialog.ShareDialogFeature.PHOTOS)) {
            // Publish the post using the Photo Share Dialog
            Utils.showToast(activity, "Calling");
            FacebookDialog shareDialog = new FacebookDialog.PhotoShareDialogBuilder(this)
                    .addPhotos(bitmapList)
                    .setPlace(AppConstants.HASH_TAG)
                    .build();
            uiHelper.trackPendingDialogCall(shareDialog.present());
        } else {
            Utils.showToast(activity, "Facebook App for android was not found in this device");
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            try {
                Bitmap bitmap;
                Uri selectedImageUri = data.getData();
                String tempPath = getPath(selectedImageUri, activity);
                BitmapFactory.Options btmapOptions = new BitmapFactory.Options();
                bitmap = BitmapFactory.decodeFile(tempPath, btmapOptions);
                compressBitmap = getResizeBitmap(bitmap, 1024);
                return true;
            } catch (Exception e) {
                Timber.e(Log.getStackTraceString(e));
                return false;
            }
        }

        uiHelper.onActivityResult(requestCode, resultCode, data, new FacebookDialog.Callback() {
            @Override
            public void onError(FacebookDialog.PendingCall pendingCall, Exception error, Bundle data) {
                Timber.e(String.format("Error: %s", error.toString()));
            }

            @Override
            public void onComplete(FacebookDialog.PendingCall pendingCall, Bundle data) {
                Timber.i("Success!");
            }
        });
    }


    public String getPath(Uri uri, Activity activity) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        Cursor cursor = activity
                .managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    public Bitmap getResizeBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();
        float bitmapRatio = (float) width / (float) height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
    }
}

代码完美无缺;但每当我选择任何图像时,图像上传都会启动但是在一秒钟内弹出一个错误对话框,说明&#34; FACEBOOK UPLOAD FAILED,你的照片无法上传&#34; logcat很干净,没有错误信息。

另外,我需要知道如何使用我提供的 #HASHTAG 在Wall Post [不在图库中]发布图像。

请帮忙!

提前致谢。

0 个答案:

没有答案