我试图通过我的应用在Twitter上分享图片。我在我的代码中使用TweetComposer
在Twitter上分享图像,但它只共享文本而不是图像。即使没有任何错误或例外。所以,我想确认是否可以在没有app的情况下在Twitter上分享图像?
答案 0 :(得分:1)
是您可以在没有应用程序的情况下在Twitter上共享图像或链接 使用此代码。首先在库中添加socialauth 4.4.jar文件,然后
private SocialAuthAdapter socialAuthAdapter;
if (Utils.isOnline(activity)) {
// Initialize the socialAuthAdapter with ResponseListener
pd = ProgressDialog.show(activity, null, null);
socialAuthAdapter = new SocialAuthAdapter(new ResponseListener(
share));
// Add Twitter to set as provider to post on twitter
socialAuthAdapter.addProvider(SocialAuthAdapter.Provider.TWITTER, R.drawable.twitter);
// this line is for Authorize start
socialAuthAdapter.authorize(activity, SocialAuthAdapter.Provider.TWITTER);
} else {
// showing message when internet connection is not available
Toast.makeText(activity,
"Check your internet connection..", Toast.LENGTH_LONG)
.show();
}
并实现一个响应侦听器
private class ResponseListener implements DialogListener {
String message;
public ResponseListener(String message) {
this.message = message;
}
@Override
public void onComplete(final Bundle values) {
// this method is call when successfull authorization is done
try {
socialAuthAdapter.updateStatus(message, new UploadImageListener(),true);
new SHARE_POST().execute();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onError(SocialAuthError error) {
// this method is call when error is occured in authorization
if (pd != null && pd.isShowing())
pd.dismiss();
Log.d("ShareTwitter", "Authentication Error: " + error.getMessage());
}
@Override
public void onCancel() {
// this method is call when user cancel Authentication
if (pd != null && pd.isShowing())
pd.dismiss();
Log.d("ShareTwitter", "Authentication Cancelled");
}
@Override
public void onBack() {
// this method is call when user backpressed from dialog
if (pd != null && pd.isShowing())
pd.dismiss();
Log.d("ShareTwitter", "Dialog Closed by pressing Back Key");
}
}
private final class UploadImageListener implements
SocialAuthListener<Integer> {
@Override
public void onError(SocialAuthError e) {
}
@Override
public void onExecute(String arg0, Integer arg1) {
Integer status = arg1;
try {
if (status.intValue() == 200 || status.intValue() == 201
|| status.intValue() == 204) {
if (pd != null && pd.isShowing())
pd.dismiss();
Toast.makeText(activity, "Image Uploaded",
Toast.LENGTH_SHORT).show();
} else {
if (pd != null && pd.isShowing())
pd.dismiss();
Toast.makeText(activity, "Image not Uploaded",
Toast.LENGTH_SHORT).show();
}
} catch (NullPointerException e) {
if (pd != null && pd.isShowing())
pd.dismiss();
Toast.makeText(activity, "Image not Uploaded",
Toast.LENGTH_SHORT).show();
}
}
}