簡體   English   中英

在 Facebook Android SDK 上分享照片

[英]Share photo on Facebook Android SDK

我正在嘗試使用 Android SDK 在 Facebook 上分享照片。

問題是我的Bitmap圖像為空。 這是我的代碼:

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);
    }

您可以嘗試從 InputStream 獲取位圖。 用這個,

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

其中 resolver 是您的ContentResolveruridata.getData();返回data.getData();

您不需要查詢數據庫,除非您想檢查任何 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. 您來自 MediaStore 的“picturePath”光標,它可能像這樣/storage/emulated/0/DCIM/Camera/IMG20200513xx.jpg
  2. 您可以嘗試更改內容 URI 的路徑( file:///storage/emulated/0/DCIM/Camera/IMG20200513xx.jpg )。
  3. 使用內容提供者的getContentResolver()打開您的圖像。 然后將圖像解碼為位圖。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM