I\'m trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.
Here is
This might be helpful for somebody:
private void sendShareTwit() {
try {
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
String filename = "twitter_image.jpg";
File imageFile = new File(Environment.getExternalStorageDirectory(), filename);
tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
tweetIntent.setType("image/jpeg");
PackageManager pm = getActivity().getPackageManager();
List<ResolveInfo> lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for (ResolveInfo ri : lract) {
if (ri.activityInfo.name.contains("twitter")) {
tweetIntent.setClassName(ri.activityInfo.packageName,
ri.activityInfo.name);
resolved = true;
break;
}
}
startActivity(resolved ?
tweetIntent :
Intent.createChooser(tweetIntent, "Choose one"));
} catch (final ActivityNotFoundException e) {
Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
}
}
Here is solution:
private fun shareOnTwitter() {
val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)
val intent = Intent(Intent.ACTION_SEND).apply {
type = "image/jpeg"
putExtra(Intent.EXTRA_STREAM, uriForFile)
}
startActivity(intent)
}
This is what you need
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);