2015-12-17 33 views
2

在這裏,我放置我的代碼任何人都可以告訴出了什麼問題?如何從SD卡上傳圖片到Facebook?

CODE

private void onFbLogin() { 
      LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("user_friends")); 
      LoginManager.getInstance().registerCallback(callbackManager, 
        new FacebookCallback<LoginResult>() { 
         @Override 
         public void onSuccess(LoginResult loginResult) { 
          System.out.println("Success"); 
          if (ShareDialog.canShow(ShareLinkContent.class)) {       
           File sdCard = Environment.getExternalStorageDirectory(); 
           File file = new File(sdCard, "final_card.jpg"); 



          ShareLinkContent content = new ShareLinkContent.Builder()// 
            .setContentUrl(Uri.parse("file:///sdcard/sharing_event.jpg")) 
            .setContentDescription("some desc") 
            .setContentTitle("Chennai") 
            .build(); 
          shareDialog.show(content); 
         } 
        } 

        @Override 
        public void onCancel() { 
         showAlert(); 
        } 

        @Override 
        public void onError(FacebookException error) { 
         showAlert(); 
        } 

        private void showAlert() { 
         new AlertDialog.Builder(FinalCardView.this) 
           .setTitle(getString(R.string.cancelled)) 
           .setMessage(getString(R.string.permission_not_granted)) 
           .setPositiveButton(getString(R.string.ok), null) 
           .show(); 
        } 
       }); 
    } 
+1

有什麼問題? –

+0

如何爲圖像共享提供本地圖像路徑 –

回答

1

終於對我有用了......沒有任何登錄和權限publish_action ......但安裝在我們的手機原生Android應用程序......

公共無效shareFacebookUsingGraph(){

ShareOpenGraphObject object = new ShareOpenGraphObject.Builder() 
    .putString("og:type", "books.book") 
    .putString("og:title", "A Game of Thrones") 
    .putString("og:description", "In the frozen wastes to the north of Winterfell, sinister and supernatural forces are mustering.") 
    .putString("books:isbn", "0-553-57340-3") 
    .build(); 

    // Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.message_box); 

    File sd = Environment.getExternalStorageDirectory(); 
     File image_file = new File(sd, "sharing_event.jpg"); 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap bitmap_image = BitmapFactory.decodeFile(image_file.getAbsolutePath(), options); 

    SharePhoto photo = new SharePhoto.Builder() 
    .setBitmap(bitmap_image) 
    .setUserGenerated(true) 
    .build(); 

// Create an action 
ShareOpenGraphAction action = new ShareOpenGraphAction.Builder() 
    .setActionType("books.reads") 
    .putObject("book", object) 
    .putPhoto("image", photo) 
    .build(); 

ShareOpenGraphContent content = new ShareOpenGraphContent.Builder() 
.setPreviewPropertyName("book") 
.setAction(action) 
.build(); 

ShareDialog.show(FinalCardView.this, content); 
} 
1

你想分享的圖片 - 它需要使用SharePhoto照片

SharePhoto photo = new SharePhoto.Builder() 
     .setBitmap(bitmap) 
     .build(); 
SharePhotoContent content = new SharePhotoContent.Builder() 
     .addPhoto(photo) 
     .build(); 

這樣你就可以得到一個位圖文件

Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()