2013-07-09 144 views

回答

0

我有从互联网上读取的很多链接的解决方案。我希望它很有用!

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); 
     private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization"; 
     private boolean pendingPublishReauthorization = false; 

    Session.openActiveSession(YOUR_ACTIVITY, true, new Session.StatusCallback() { 
       // callback when session changes state 
       @Override 
       public void call(final Session session, SessionState state, Exception exception) { 
        if (session.isOpened()) { 
        Session session = Session.getActiveSession(); 

      if (session != null){ 

       // Check for publish permissions  
       List<String> permissions = session.getPermissions(); 
       if (!isSubsetOf(PERMISSIONS, permissions)) { 
        pendingPublishReauthorization = true; 
        Session.NewPermissionsRequest newPermissionsRequest = new Session 
          .NewPermissionsRequest(this, PERMISSIONS); 
        session.requestNewPublishPermissions(newPermissionsRequest); 
        return; 
       } 
       Bitmap b = YOUR_BITMAP; 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
        b.compress(Bitmap.CompressFormat.PNG, 100, stream); 
        byte[] byteArray = stream.toByteArray(); 


       Bundle postParams = new Bundle(); 
       postParams.putString("name", "Facebook SDK for Android"); 
       postParams.putByteArray("picture", byteArray); 
       Request.Callback callback= new Request.Callback() { 
        public void onCompleted(Response response) { 
         JSONObject graphResponse = response 
                .getGraphObject() 
                .getInnerJSONObject(); 
         String postId = null; 
         try { 
          postId = graphResponse.getString("id"); 
         } catch (JSONException e) { 
          Log.i("error", 
           "JSON error "+ e.getMessage()); 
         } 
         FacebookRequestError error = response.getError(); 
         if (error != null) { 
          Toast.makeText(getActivity() 
           .getApplicationContext(), 
           error.getErrorMessage(), 
           Toast.LENGTH_SHORT).show(); 
          } else { 
           Toast.makeText(getActivity() 
            .getApplicationContext(), 
            postId, 
            Toast.LENGTH_LONG).show(); 
         } 
        } 
       }; 

       Request request = new Request(session, "me/photos", postParams, 
             HttpMethod.POST, callback); 

       RequestAsyncTask task = new RequestAsyncTask(request); 
       task.execute(); 
      } 


       } 
      } 
      }); 



private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) { 
    for (String string : subset) { 
     if (!superset.contains(string)) { 
      return false; 
     } 
    } 
    return true; 
} 
相关问题