简体   繁体   中英

Share photo on Facebook Android SDK

I'm trying to share a photo on Facebook with Android SDK.

The problem is that my Bitmap image is null. This is my code:

public void sharePhoto(View view) {
        if (Session.getInstance().getUserFace().getBestphoto() == null) {
            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();
            Log.d("picture path", picturePath);
            BitmapFactory.Options options = new BitmapFactory.Options();
            try {
                Bitmap image = BitmapFactory.decodeFile(picturePath, options);
                uploadPhoto(image);
            } catch (OutOfMemoryError e) {
                try {
                    options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
                    Bitmap image = BitmapFactory.decodeFile(picturePath, options);
                    uploadPhoto(image);
                } catch (Exception ex) {
                    Log.e("EXCEPTION", ex.toString());
                }
            }
        }
    }

    private void uploadPhoto(Bitmap image) {
        Log.d("Image", "" + image);
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(image)
                .build();
        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .build();
        ShareDialog.show(MainActivity.this, content);
    }

You can try getting the bitmap from InputStream instead. Use this,

BitmapFactory.decodeStream(resolver.openInputStream(uri), null, options);

where resolver is your ContentResolver and uri is returned by data.getData();

You won't need to query the database, unless you wanna check for any FileNotFoundException.

File file = new File(picturePath);
Uri imageUri = Uri.fromFile(file.getAbsoluteFile()); 
try {
    Bitmap image = BitmapFactory.decodeStream(mContext.getContentResolver().openInputStream(imageUri));
    uploadPhoto(image); 
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
  1. Your "picturePath" cursor from MediaStore, it maybe like this /storage/emulated/0/DCIM/Camera/IMG20200513xx.jpg .
  2. You can try to changing path to content URI ( file:///storage/emulated/0/DCIM/Camera/IMG20200513xx.jpg ).
  3. Use content provider's getContentResolver() to open your image. Then decode image to Bitmap.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM