无法在Android应用中与Facebook分享照片

时间:2017-04-12 08:55:45

标签: java android facebook

我正试图通过Android应用程序在Facebook上分享照片。我从图库中获取图像并使用它创建一个位图,然后使用setShareContent方法将其发送到Facebook。

运行应用程序时,日志会显示:

  

意外活动暂停,但没有匹配的活动恢复。记录数据可能不正确。请务必从应用的activateApp方法中致电onCreate()

我有以下代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ShareButton shareButton;
    FloatingActionButton selectImageFromGalleryBtn;
    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        shareButton = (ShareButton) findViewById(R.id.shareButton);
        selectImageFromGalleryBtn = (FloatingActionButton) findViewById(R.id.selectImageFromGalleryBtn);

        selectImageFromGalleryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, 0);
            }
        });

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

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

        shareButton.setShareContent(content);
    }
}

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dchurches.sharevideofacebook">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/MyMaterialTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

        <provider
               android:authorities="com.facebook.app.FacebookContentProviderXXXXXXXXXXXXXXX"
            android:name="com.facebook.FacebookContentProvider"
            android:exported="true"/>
    </application>

</manifest>

我按照Facebook开发人员的文档分享了一张照片,但它似乎没有用。有谁能够帮我?提前致谢

1 个答案:

答案 0 :(得分:0)

您需要编写一个扩展应用程序并初始化FB和Logger的类,如下所示

public class AppCtrl extends Application {

@Override
public void onCreate() {
   super.onCreate();
   // Initialize the SDK before executing any other operations,
   FacebookSdk.sdkInitialize(getApplicationContext());
   AppEventsLogger.activateApp(this);
   }   
}

并在manifest的应用程序标记中定义此类,如下所示

    <application android:name="AppCtrl"
       ...>
    </application>