简体   繁体   中英

Can't share a photo in Facebook from Android app

I am trying to share a photo on Facebook from an Android application. I take the image from the gallery and create a bitmap with it, which I then send to Facebook with the setShareContent method.

When you run the app, the log says this:

Unexpected activity pause without a matching activity resume. Logging data may be incorrect. Make sure you call activateApp from your Application's onCreate() method.

I have the following code:

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>

I followed the Facebook developers documentation to share a photo, but it does not seem to work. Can anybody help me? Thanks in advance

You need to write a class that extends application and initialize FB and Logger as below

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);
   }   
}

and define this class in manifest's application tag as below

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM