Android-Facebook SDK:无法共享照片,对话框无法显示

前端之家收集整理的这篇文章主要介绍了Android-Facebook SDK:无法共享照片,对话框无法显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我第一次使用 Android-Facebook SDK.我想发布一个位图,但我继续收到这条消息:
  1. Warning: Error: com.facebook.FacebookException: Failed to copy image.

我可以发布简单的链接,但不能发布带照片的链接.

我该怎么解决这个问题?另外,我注意到的一件事是没有调用StatusCallback.call().

  1. public abstract class FacebookReadyBaseActivity extends Activity {
  2. private static final String TAG = "FacebookReadyBaseActivity";
  3.  
  4. private UiLifecycleHelper uiHelper;
  5.  
  6. protected PendingAction pendingAction = PendingAction.NONE;
  7. private static final String PERMISSION = "publish_actions";
  8. protected boolean canPresentShareDialogWithPhotos;
  9. private boolean canPresentShareDialog;
  10. private GraphUser user;
  11. private GraPHPlace place;
  12. private List<GraphUser> tags;
  13. protected Bitmap pendingBitmap;
  14.  
  15. protected enum PendingAction {
  16. NONE,POST_PHOTO,POST_STATUS_UPDATE
  17. }
  18.  
  19. private Session.StatusCallback callback = new Session.StatusCallback() {
  20. @Override
  21. public void call(Session session,SessionState state,Exception exception) {
  22. onSessionStateChange(session,state,exception);
  23. }
  24. };
  25.  
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29.  
  30. uiHelper = new UiLifecycleHelper(this,callback);
  31. uiHelper.onCreate(savedInstanceState);
  32.  
  33. canPresentShareDialog = FacebookDialog.canPresentShareDialog(this,FacebookDialog.ShareDialogFeature.SHARE_DIALOG);
  34. canPresentShareDialogWithPhotos = FacebookDialog.canPresentShareDialog(
  35. this,FacebookDialog.ShareDialogFeature.PHOTOS);
  36. }
  37.  
  38. @Override
  39. protected void onActivityResult(int requestCode,int resultCode,Intent data) {
  40. super.onActivityResult(requestCode,resultCode,data);
  41.  
  42. uiHelper.onActivityResult(requestCode,data,new FacebookDialog.Callback() {
  43. @Override
  44. public void onError(FacebookDialog.PendingCall pendingCall,Exception error,Bundle data) {
  45. Log.e(TAG,String.format("Error: %s",error.toString()));
  46. }
  47.  
  48. @Override
  49. public void onComplete(
  50. FacebookDialog.PendingCall pendingCall,Bundle data) {
  51. Log.i(TAG,"Success!");
  52. }
  53. });
  54. }
  55.  
  56. @Override
  57. protected void onResume() {
  58. super.onResume();
  59. uiHelper.onResume();
  60. }
  61.  
  62. @Override
  63. protected void onSaveInstanceState(Bundle outState) {
  64. super.onSaveInstanceState(outState);
  65. uiHelper.onSaveInstanceState(outState);
  66. }
  67.  
  68. @Override
  69. public void onPause() {
  70. super.onPause();
  71. uiHelper.onPause();
  72. }
  73.  
  74. @Override
  75. public void onDestroy() {
  76. super.onDestroy();
  77. uiHelper.onDestroy();
  78. }
  79.  
  80. protected UiLifecycleHelper getFacebookUiHelper() {
  81. return uiHelper;
  82. }
  83.  
  84. protected void performPublish(PendingAction action,boolean allowNoSession) {
  85. Session session = Session.getActiveSession();
  86. if (session != null) {
  87. pendingAction = action;
  88. if (hasPublishPermission()) {
  89. // We can do the action right away.
  90. handlePendingAction();
  91. return;
  92. } else if (session.isOpened()) {
  93. // We need to get new permissions,then complete the action when
  94. // we get called back.
  95. session.requestNewPublishPermissions(new Session.NewPermissionsRequest(
  96. this,PERMISSION));
  97. return;
  98. }
  99. }
  100.  
  101. if (allowNoSession) {
  102. pendingAction = action;
  103. handlePendingAction();
  104. }
  105. }
  106.  
  107. private boolean hasPublishPermission() {
  108. Session session = Session.getActiveSession();
  109. return session != null
  110. && session.getPermissions().contains("publish_actions");
  111. }
  112.  
  113. @SuppressWarnings("incomplete-switch")
  114. private void handlePendingAction() {
  115. PendingAction prevIoUslyPendingAction = pendingAction;
  116. // These actions may re-set pendingAction if they are still pending,but
  117. // we assume they
  118. // will succeed.
  119. pendingAction = PendingAction.NONE;
  120.  
  121. switch (prevIoUslyPendingAction) {
  122. case POST_PHOTO:
  123. postPhoto();
  124. break;
  125. case POST_STATUS_UPDATE:
  126. postStatusUpdate();
  127. break;
  128. }
  129. }
  130.  
  131. private void postPhoto() {
  132.  
  133. if (canPresentShareDialogWithPhotos) {
  134. FacebookDialog shareDialog = createShareDialogBuilderForPhoto(
  135. pendingBitmap).build();
  136. uiHelper.trackPendingDialogCall(shareDialog.present());
  137. } else if (hasPublishPermission()) {
  138. Request request = Request.newUploadPhotoRequest(
  139. Session.getActiveSession(),pendingBitmap,new Request.Callback() {
  140. @Override
  141. public void onCompleted(Response response) {
  142. InfoDialog.newInstance(
  143. FacebookReadyBaseActivity.this,"Done","Photo share finished.",null).show();
  144. }
  145. });
  146. request.executeAsync();
  147. } else {
  148. pendingAction = PendingAction.POST_PHOTO;
  149. }
  150. }
  151.  
  152. private void postStatusUpdate() {
  153. if (canPresentShareDialog) {
  154. FacebookDialog shareDialog = createShareDialogBuilderForLink()
  155. .build();
  156. uiHelper.trackPendingDialogCall(shareDialog.present());
  157. } else if (user != null && hasPublishPermission()) {
  158. // TODO fixme
  159. final String message = "Status update finished.";
  160. Request request = Request.newStatusUpdateRequest(
  161. Session.getActiveSession(),message,place,tags,new Request.Callback() {
  162. @Override
  163. public void onCompleted(Response response) {
  164.  
  165. InfoDialog.newInstance(
  166. FacebookReadyBaseActivity.this,null).show();
  167. }
  168. });
  169. request.executeAsync();
  170.  
  171. } else {
  172. pendingAction = PendingAction.POST_STATUS_UPDATE;
  173. }
  174. }
  175.  
  176. private FacebookDialog.PhotoShareDialogBuilder createShareDialogBuilderForPhoto(
  177. Bitmap... photos) {
  178. return new FacebookDialog.PhotoShareDialogBuilder(this)
  179. .addPhotos(Arrays.asList(photos));
  180. }
  181.  
  182. private FacebookDialog.ShareDialogBuilder createShareDialogBuilderForLink() {
  183. return new FacebookDialog.ShareDialogBuilder(this)
  184. .setName("Hello Facebook")
  185. .setDescription(
  186. "The 'Hello Facebook' sample application showcases simple Facebook integration")
  187. .setLink("http://developers.facebook.com/android");
  188. }
  189.  
  190.  
  191. private void onSessionStateChange(Session session,Exception exception) {
  192. if (pendingAction != PendingAction.NONE
  193. && (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException)) {
  194. InfoDialog.newInstance(FacebookReadyBaseActivity.this,"Failed","Not granted.",null).show();
  195. pendingAction = PendingAction.NONE;
  196. } else if (state == SessionState.OPENED_TOKEN_UPDATED) {
  197. handlePendingAction();
  198. }
  199.  
  200. }
  201.  
  202. }

解决方法

终于搞定了!

在提供的HelloFacebookSample项目中,我在清单中看到了这个:

  1. <provider android:authorities="com.facebook.app.NativeAppCallContentProvider355198514515820"
  2. android:name="com.facebook.NativeAppCallContentProvider"
  3. android:exported="true"/>

我只是使用自己的Facebook应用程序ID更改了数字部分.不知道为什么我没有在文档中看到这一点.

编辑
之后发现:https://developers.facebook.com/docs/reference/android/current/class/NativeAppCallContentProvider/

猜你在找的Android相关文章