Paste
Pasted as Plain Text by kalai ( 13 years ago )
public class ShareImage extends Activity {
public String imageUrls;
private Facebook facebook;
private String messageToPost;
private static final String APP_ID = "YOUR APP ID";
private static final String[] PERMISSIONS = new String[] { "publish_stream" };
private static final String TOKEN = "access_token";
private static final String EXPIRES = "expires_in";
private static final String KEY = "facebook-credentials";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = getIntent().getExtras();
imageUrls = bundle.getString("Image");
facebook = new Facebook(APP_ID);
restoreCredentials(facebook);
messageToPost = "Hello Everyone.";
if (!facebook.isSessionValid()) {
loginAndPostToWall();
} else {
// postToWall(messageToPost);
ShareImage1(imageUrls);
}
}
public boolean saveCredentials(Facebook facebook) {
Editor editor = getApplicationContext().getSharedPreferences(KEY,
Context.MODE_PRIVATE).edit();
editor.putString(TOKEN, facebook.getAccessToken());
editor.putLong(EXPIRES, facebook.getAccessExpires());
return editor.commit();
}
public boolean restoreCredentials(Facebook facebook) {
SharedPreferences sharedPreferences = getApplicationContext()
.getSharedPreferences(KEY, Context.MODE_PRIVATE);
facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
return facebook.isSessionValid();
}
public void loginAndPostToWall() {
facebook.authorize(this, PERMISSIONS,Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}
public void ShareImage1(String image)
{
Bundle params = new Bundle();
params.putString("caption", "Beautify your Screen-Image");
params.putString("description","The Image of Beautify your Screen...");
params.putString("picture",image);
params.putString("name", "Beautify your Screen-Image");
facebook.dialog(this, "feed", params, new DialogListener() {
public void onComplete(Bundle values) {
String check;
check=values.toString();
if(check.equals("Bundle[{}]"))
{
showToast("Canceled the image to post!");
finish();
}
else
{
showToast("Image has been posted to your facebook wall!");
finish();
}
}
public void onCancel() {
showToast("Canceled the image to post to your facebook wall!");
}
public void onError(DialogError de) {
showToast("Canceled the image to post to your facebook wall!");
}
public void onFacebookError(FacebookError fbe) {
showToast("Canceled the image to post to your facebook wall!");
}
});
}
public void postToWall(String message) {
Bundle parameters = new Bundle();
parameters.putString("message", message);
parameters.putString("description", "topic share");
try {
facebook.request("me");
String response = facebook.request("me/feed", parameters, "POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("")
|| response.equals("false")) {
showToast("Blank response.");
} else {
showToast("Message posted to your facebook wall!");
}
} catch (Exception e) {
showToast("Failed to post to wall!");
e.printStackTrace();
}
}
class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
saveCredentials(facebook);
if (messageToPost != null) {
// postToWall(messageToPost);
ShareImage1(imageUrls);
}
}
public void onFacebookError(FacebookError error) {
showToast("Authentication with Facebook failed!");
}
public void onError(DialogError error) {
showToast("Authentication with Facebook failed!");
}
public void onCancel() {
showToast("Authentication with Facebook cancelled!");
}
}
private void showToast(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT)
.show();
}
}
Revise this Paste