SctALE
SctALE

Reputation: 517

Post photo facebook android

I would create a post with a photo and a title in my app and post it on my facebook. I don't know exactly the steps that are needed, so i don't know what i am doing wrong. The main activity has in its xml a LoginButton and a ShareButton. I did:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
public DBhelper database;
public CallbackManager callbackManager;
public ShareDialog shareDialog;
public ShareButton shareButton;
public SharePhotoContent sharePhotoContent;

}

This class have:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    AppEventsLogger.activateApp(this);
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);
    shareButton=new ShareButton(this);

    setContentView(R.layout.activity_main);

    LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
    loginButton.setReadPermissions("user_friends","public_profile");
    get_login_details(loginButton);

    shareButton=(ShareButton) findViewById(R.id.share_button);
    sharePhotoContent=genera_share_photo_content();
    shareButton.setShareContent(sharePhotoContent);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode,resultCode,data);
    Log.e(TAG,data.toString());

}

And these two methods:

protected void get_login_details(LoginButton loginButton) {
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Toast.makeText(getApplicationContext(),"SI",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancel() {
            // App code
            Toast.makeText(getApplicationContext(),"CANCEL",Toast.LENGTH_LONG).show();
        }

        @Override
        public void onError(FacebookException exception) {
            // App code
            Toast.makeText(getApplicationContext(),"NO",Toast.LENGTH_LONG).show();
        }
    });

}

public SharePhotoContent genera_share_photo_content() {
    //code...
    Bitmap image = ...

    SharePhoto photo = new SharePhoto.Builder()
            .setBitmap(image)
            .build();
    SharePhotoContent content = new SharePhotoContent.Builder()
            .addPhoto(photo)
            .build();
    return content;

}

Now, the share content is maked at the beginning: i would make it dynamic using genera_share_photo_content(): how can i modify it when i want? Should i call somewhere shareDialog.show(content) or ShareApi.share(content,null)? Are the permission user_friends and public_profile , that i set for the login button. enought to make all work?

Upvotes: 0

Views: 100

Answers (1)

Ashwani
Ashwani

Reputation: 1304

you have to add permission that is:

"publish_actions"

and in your manifest you have to include these tags

<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/facebook_app_id" />
    <provider
        android:name="com.facebook.FacebookContentProvider"
        android:authorities="com.facebook.app.FacebookContentProvider1618161851808217"
        android:exported="true" />
    <activity
        android:name="com.facebook.FacebookActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Translucent.NoTitleBar" />

Do this:

private void sharePhotoToFacebook(){

    String filePath = "path to your image";
    String text = "your custom text";
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
        SharePhoto photo = new SharePhoto.Builder()
                .setBitmap(bitmap)
                .setCaption(text)
                .build();

        SharePhotoContent content = new SharePhotoContent.Builder()
                .addPhoto(photo)
                .setContentUrl(Uri.parse(text))
                .build();
    ShareApi.share(content,null);
    Toast.makeText(getApplicationContext(),"Snapshot shared On Facebook",Toast.LENGTH_SHORT).show();

}

public void publishimage() {
    Log.i("Android","publishing Image");
    FacebookSdk.sdkInitialize(getApplicationContext());

    callbackManager = CallbackManager.Factory.create();

    shareDialog = new ShareDialog(this);
    java.util.List<String> permissionNeeds = Arrays.asList("publish_actions");

    //this loginManager helps you eliminate adding a LoginButton to your UI
    manager = LoginManager.getInstance();

    manager.logInWithPublishPermissions(this, permissionNeeds);

    manager.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            sharePhotoToFacebook();
        }

        @Override
        public void onCancel() {
            System.out.println("onCancel");
        }

        @Override
        public void onError(FacebookException exception) {
            System.out.println("onError");
        }
    });

}

and on button click that is:

btnshare.setOnclicklistener(new View.OnclickListener){
    @Override
    public void onClick(View V){
        publishimage();
    }
}

Upvotes: 1

Related Questions