使用Facebook sdk v4.x选择进入Facebook后,从图库分享图像

时间:2015-08-21 03:53:08

标签: android facebook facebook-sdk-4.0

我想在选择之后将我的图像从Gallery分享到Facebook,我正在使用Facebook sdk v4.0。这是我的代码,我不知道为什么postPhoto()中的Bitmap仍为null,但pickImage()中的fileUri已经有文件路径。

这是选择图像意图

 private void pickImage() {
    Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getIntent.setType("image/*");

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");

    Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});

    startActivityForResult(chooserIntent, PICK_PHOTO_FROM_GALLERY_REQUEST_CODE);

}

之后我将fileUri命名为

 public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

接下来,我有postPhoto(),我可以使用facebook sdk分享照片

private void onClickPostPhoto() {
    performPublish(PendingAction.POST_PHOTO, canPresentShareDialogWithPhotos);
}

private void postPhoto() {

    Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath()); //>>>bitmap still null PLEAS HELP!!!
    SharePhoto sharePhoto = new SharePhoto.Builder().setBitmap(bitmap).build();
    ArrayList<SharePhoto> photos = new ArrayList<>();
    photos.add(sharePhoto);

    SharePhotoContent sharePhotoContent =
            new SharePhotoContent.Builder().setPhotos(photos).build();
    if (canPresentShareDialogWithPhotos) {
        shareDialog.show(sharePhotoContent);
    } else if (hasPublishPermission()) {
        ShareApi.share(sharePhotoContent, shareCallback);
    } else {
        pendingAction = PendingAction.POST_PHOTO;
    }


}

最后,我得到了onActivityForResult

  callbackManager.onActivityResult(requestCode, resultCode, data);
    //******Camera********
    if(requestCode == PICK_PHOTO_FROM_GALLERY_REQUEST_CODE && resultCode == RESULT_OK){
            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);
            Log.d("TAG" + ">>>>>>>>>>>>>>>>>>", picturePath);
            cursor.close();
        fileUri = Uri.fromFile(new File(picturePath));
        onClickPostPhoto();

最后,几个小时后。我修复了它,在onActivityForResult上看到我上面的编辑。

0 个答案:

没有答案