2016-08-01 5 views
0

Android SDK가있는 Facebook에서 사진을 공유하려고합니다.사진을 Facebook에 공유 Android SDK

문제는 내 Bitmap 이미지가 null입니다. 이것은 내 코드입니다 :

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

답변

0

대신에 InputStream에서 비트 맵을 가져올 수 있습니다. 이 사용

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

해결이

당신의 ContentResolveruri 당신은 어떤 FileNotFoundException이 들어,하지 않는 한 싶어 확인 데이터베이스를 쿼리 할 필요가 없습니다 data.getData();

에 의해 반환됩니다.