Reputation: 159
Following this easy code:
String tweetUrl = "https://twitter.com/intent/tweet?text=PUT TEXT HERE &url="+"https://www.google.com";
Uri uri = Uri.parse(tweetUrl);
startActivity(new Intent(Intent.ACTION_VIEW, uri));
Its possible to share also with those text and image? Without using twitter4j
library.
Thank you!
Upvotes: 0
Views: 972
Reputation: 2823
Try this it is worked for me. I will use it for share my product details. i will get details of product like name,image,description from the server and display in app and then share it to twitter.
Get image url from server.
URL url = ConvertToUrl(imgURL);
Bitmap imagebitmap = null;
try {
imagebitmap = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Post image and text on twitter.
// post on twitter
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(
Intent.EXTRA_TEXT,
"Check this out, what do you think?"
+ System.getProperty("line.separator")
+ sharedescription);
intent.putExtra(Intent.EXTRA_SUBJECT, sharename);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM,
getImageUri(HomeActivity.this, imagebitmap));
intent.setPackage("com.twitter.android");
startActivity(intent);
Convert Uri String to URL
private URL ConvertToUrl(String urlStr) {
try {
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(),
url.getHost(), url.getPort(), url.getPath(),
url.getQuery(), url.getRef());
url = uri.toURL();
return url;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Convert Bitmap to Uri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(
inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Upvotes: 2
Reputation: 23638
What this code does is to query all activities that match with the Intent.ACTION_SEND
and then it searches for the “com.twitter.android.PostActivity”
intent.
try{
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("text/plain");
final PackageManager pm = context.getPackageManager();
final List activityList = pm.queryIntentActivities(intent, 0);
int len = activityList.size();
for (int i = 0; i < len; i++) {
final ResolveInfo app = activityList.get(i);
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
final ActivityInfo activity=app.activityInfo;
final ComponentName name=new ComponentName(activity.applicationInfo.packageName, activity.name);
intent=new Intent(Intent.ACTION_SEND);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.setComponent(name);
intent.putExtra(Intent.EXTRA_TEXT, message);
context.startActivity(intent);
break;
}
}
} catch(final ActivityNotFoundException e) {
Log.i("mufumbo", "no twitter native",e );
}
Upvotes: 0