2013-08-24 163 views
1

我想在Facebook上分享手機SD卡上的圖像。Facebook和Android sdk

我的問題是我運行應用程序,我可以看到下面的消息

所有MyApplication想訪問您的公開資料和好友列表之後。

但是,當我按下確定,我去我的Facebook頁面,我看不到任何圖像。

我下面就this link的指示,我的代碼如下

public class PhotoActivity extends BaseFragmentActivity { 
    private static final String MIME_TYPE = "image/jpeg"; 

    private Uri uri; 
    private ImageView mPhotoView; 

    private Session.StatusCallback mStatusCallback = new SessionStatusCallback(); 
    private boolean mPostToWall = false; 

    private ImageButton retakeBtn; 

    private class SessionStatusCallback implements Session.StatusCallback { 
     @Override 
     public void call(Session session, SessionState state, 
       Exception exception) { 

      if (session.isOpened() && mPostToWall) { 
       share(); 

      } 
     } 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Session.getActiveSession().addCallback(mStatusCallback); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Session.getActiveSession().removeCallback(mStatusCallback); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Session.getActiveSession().onActivityResult(this, requestCode, 
       resultCode, data); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     Session session = Session.getActiveSession(); 
     Session.saveSession(session, outState); 
    } 

    private void share() { 


     Bundle bundle = new Bundle(); 
     mPhotoView.setDrawingCacheEnabled(true); 
     mPhotoView.buildDrawingCache(); 
     Bitmap bmap = mPhotoView.getDrawingCache(); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bmap.compress(Bitmap.CompressFormat.JPEG, 80, stream); 
     byte[] byteArray = stream.toByteArray(); 

     bundle.putByteArray("picture", byteArray); 


      WebDialog feedDialog = (
       new WebDialog.FeedDialogBuilder(getApplicationContext(), 
        Session.getActiveSession(), 
        bundle)) 
       .setOnCompleteListener(new OnCompleteListener() { 

        @Override 
        public void onComplete(Bundle values, 
         FacebookException error) { 
         if (error == null) { 
          // When the story is posted, echo the success 
          // and the post Id. 
          final String postId = values.getString("post_id"); 
          if (postId != null) { 
           Toast.makeText(getApplicationContext(), 
            "Posted story, id: "+postId, 
            Toast.LENGTH_SHORT).show(); 
          } else { 
           // User clicked the Cancel button 
           Toast.makeText(getApplicationContext(), 
            "Publish cancelled", 
            Toast.LENGTH_SHORT).show(); 
          } 
         } else if (error instanceof FacebookOperationCanceledException) { 
          // User clicked the "x" button 
          Toast.makeText(getApplicationContext(), 
           "Publish cancelled", 
           Toast.LENGTH_SHORT).show(); 
         } else { 
          // Generic, ex: network error 
          Toast.makeText(getApplicationContext(), 
           "Error posting story", 
           Toast.LENGTH_SHORT).show(); 
         } 
        } 

       }) 
       .build(); 
      feedDialog.show(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setTheme(R.style.AppTheme); 

     uri = getIntent().getData(); 

     setContentView(R.layout.activity_photo); 

     retakeBtn = (ImageButton) findViewById(R.id.retake_btn); 

     String lang = getString(R.string.lang); 
     if (lang.endsWith("FR")) { 
      retakeBtn.setImageResource(R.drawable.retake_xhdpi); 
     } else { 
      retakeBtn.setImageResource(R.drawable.retake_en_xhdpi); 

     } 

     // Facebook 
     Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); 

     Session session = Session.getActiveSession(); 
     if (session == null) { 
      if (savedInstanceState != null) { 
       session = Session.restoreSession(this, null, mStatusCallback, 
         savedInstanceState); 
      } 
      if (session == null) { 
       session = new Session(this); 
      } 
      Session.setActiveSession(session); 
      if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { 

       session.openForPublish(new Session.OpenRequest(PhotoActivity.this) 
         .setCallback(mStatusCallback).setPermissions(Arrays.asList("photo_upload","publish_stream","publish_actions"))); 


      } 
     } 

     mPhotoView = (ImageView) findViewById(R.id.photo); 
     mPhotoView.setImageURI(uri); 

     retakeBtn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent intent = new Intent(PhotoActivity.this, 
         CameraActivity.class); 
       startActivity(intent); 

       finish(); 
      } 
     }); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
     com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater(); 
     inflater.inflate(R.menu.item_photo_activity, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(
      com.actionbarsherlock.view.MenuItem item) { 
     switch (item.getItemId()) { 
     case android.R.id.home: 
      Intent intent = new Intent(PhotoActivity.this, CameraActivity.class); 
      intent.putExtra("from_activity", 300); 
      startActivity(intent); 
      finish(); 
      break; 

     case R.id.share_button: 
      Session session = Session.getActiveSession(); 

      if (!session.isOpened() && !session.isClosed()) { 
       session.openForPublish(new Session.OpenRequest(PhotoActivity.this) 
         .setCallback(mStatusCallback).setPermissions(Arrays.asList("photo_upload","publish_stream","publish_actions"))); 


      } else { 
       Session.openActiveSession(PhotoActivity.this, true, 
         mStatusCallback); 
      } 
      break; 
     } 
     return true; 
    } 

回答

1

您正在嘗試向Feed對話框添加一張照片,該對話框不包含二進制數據。請參閱feed dialog documentation here,「圖片」參數僅佔用一個URL。

如果要上傳照片,您需要獲取「publish_actions」權限,並調用Request.newUploadPhotoRequest方法。看看隨SDK提供的Hello Facebook示例應用程序,它演示如何上傳照片。

1

要確保你已經給了所有權限的權利。

我想你忘了添加一些權限,如:「photo_upload」,「publish_stream」,「publish_actions」。

授予此權限並嘗試它,可能會起作用。

+0

嘿,我嘗試你的建議,但它仍然不工作,你可以看看我的代碼,並幫助我 – Nik