I made an application with a feature to share news with social media
where in there is news content and news images. I've tried to follow some tutorials but still can not do it successfully.
So far the news content is sent without image. This my code:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("*/*");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, tittle_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, catagory_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, date_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, news_selected);
sharingIntent.putExtra(Intent.EXTRA_STREAM, image_selected);
startActivity(Intent.createChooser(sharingIntent,"Share using"));
Do it like this :
ImageView image = (ImageView) findViewById(R.id.yourImage);
final Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
try{
new AsyncTask<String, String, String>(){
#Override
protected String doInBackground(String... params){
String url = null;
try{
url= MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, o.getHeading(), o.getDescription());
} catch (Exception e){
e.printStackTrace();
}
return url;
}
#Override
public void onPostExecute(String url){
if(url != null){
Intent sharingIntent = new Intent();
sharingIntent.setAction(Intent.ACTION_SEND);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, tittle_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, catagory_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, date_selected);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, news_selected);
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sharingIntent, "Share"));
} else {
Toast.makeText(context, "Oops, a problem occurred while sharing. Check permission for this app.", Toast.LENGTH_LONG).show();
}
}
}.execute();
} catch (Exception e){
e.printStackTrace();
}
This is how intent is used in sharing data to any media
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to)));
Related
Share in Navigation bar all worked perfect but in WhatsApp it's only display link not text.
All other apps worked perfect telegram,mail etc... But in WhatsApp it's only display link not upper text.
try {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
final String appPackageName = getApplicationContext().getPackageName();
String strAppLink = "https://play.google.com/store/apps/details?id=" + appPackageName;
shareIntent.setType("text/plain");
String shareBody = strAppLink;
String shareSub = "Hey Download this App Called\n Appname ........\nAt least One Time Try This\n";
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(shareIntent, "Share using"));
}
catch (ActivityNotFoundException e)
{
}
Try this
In java:
try {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
final String appPackageName = getApplicationContext().getPackageName();
String strAppLink = "https://play.google.com/store/apps/details?id=" + appPackageName;
shareIntent.setType("text/plain");
String shareBody = strAppLink;
String shareSub = "Hey Download this App Called\n Appname ........\nAt least One Time Try This\n";
String data = shareSub + shareBody
// shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub);
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, data);
startActivity(Intent.createChooser(shareIntent, "Share using"));
} catch (ActivityNotFoundException e) {
}
In kotlin:
try {
val shareIntent = Intent(android.content.Intent.ACTION_SEND)
val appPackageName = applicationContext.packageName
val strAppLink = "https://play.google.com/store/apps/details?id=$appPackageName"
shareIntent.type = "text/plain"
val shareSub = "Hey Download this App Called\n Appname ........\nAt least One Time Try This\n"
val data = shareSub + strAppLink
//shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub)
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, data)
startActivity(Intent.createChooser(shareIntent, "Share using"))
} catch (e: ActivityNotFoundException) {
}
There is only one change to make is comment the line which sets the subject. Merge the subject text with text and set that merged text in android.content.Intent.EXTRA_TEXT.
It works.
I need to be able to send text to a whatsapp contact, stack overflow seems to unanimously agree that this works:
Uri uri = Uri.parse("smsto:" + "<number>");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "YOOOH");
// sendIntent.setType("text/plain");
sendIntent.setPackage("com.whatsapp");
startActivity(sendIntent);
If .setType("text/plain"); is commented out, it just opens whatsapp to the chat of the number I gave it, but if I don't comment it out, nothing happens at all, any help appreciated.
change and add the last line remove the comment of sendIntent.setType
Uri uri = Uri.parse("smsto:" + "<number>");
Intent sendIntent = new Intent(Intent.ACTION_SENDTO, uri);
sendIntent.putExtra(Intent.EXTRA_TEXT, "YOOOH");
sendIntent.setType("text/plain");
// this line helps to open the chooser dialog
startActivity(Intent.createChooser(sendIntent, getResources().getString(R.string.share)));
Look at this code snippet that one also handled case if user not installed
whatsApp.
PackageManager pm=getPackageManager();
try {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(intent, "Share with"));
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
gen_btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view){
String to = text.getText().toString().trim();
String subject = text2.getText().toString().trim();
String content = text3.getText().toString().trim();
StringBuffer buffer = new StringBuffer();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_SUBJECT, subject);
email.putExtra(Intent.EXTRA_EMAIL,to );
email.putExtra(Intent.EXTRA_TEXT, content);
email.setType("message/rfc822");
startActivity(email);
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(
email.getAction(), BarcodeFormat.QR_CODE, 250, 250
);
BarcodeEncoder barcodeEncoder=new BarcodeEncoder();
Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
image.setImageBitmap(bitmap);
}
catch (WriterException e){
e.printStackTrace();
}
}
});
i want to generate an email, as you can see, i got three edit text(to, subject, content) and i already insert the email intent, when i generate it catch all the string and i send the string through email app, but the problem is, all of my string does not get to their respective email format. where did i do wrong? sorry if you cant understand what i am asking all i can do is to show my code. i am new to android java so please help.
If I understood correctly you are trying to send email through the email app. This might be helpful.
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("plain/text");
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
startActivity(Intent.createChooser(intent, ""));
I tried this code,
I chose Facebook app and went to post page, but the chosen text does not get displayed.
public void onShareClick(View v){
List<Intent> targetShareIntents=new ArrayList<Intent>();
Intent shareIntent=new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos=getPackageManager().queryIntentActivities(shareIntent, 0);
if(!resInfos.isEmpty()){
System.out.println("Have package");
for(ResolveInfo resInfo : resInfos){
String packageName=resInfo.activityInfo.packageName;
Log.i("Package Name", packageName);
if( packageName.contains("com.facebook.katana")){
Intent intent=new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Text");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if(!targetShareIntents.isEmpty()){
System.out.println("Have Intent");
Intent chooserIntent=Intent.createChooser(targetShareIntents.remove(0), "Choose app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}else{
System.out.println("Do not Have Intent");
showDialaog(this);
}
}
}
Why doesn't the chosen text to share Display on facebook? I read Facebook's policy and they do not allow this, yet other applications are able to do so. Is there some way I can achieve that?
Try this:
public void setupFacebookShareIntent() {
ShareDialog shareDialog;
FacebookSdk.sdkInitialize(getApplicationContext());
shareDialog = new ShareDialog(this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("YOUR TITLE")
.setContentDescription("YOUR DESCRIPTION")
.setContentUrl(Uri.parse("http://xxxx.com/"))
.setImageUrl(Uri.parse("http://xxxx.com/"))
.build();
shareDialog.show(linkContent);
}
I want to share images from my application to Whatsapp but I am not able to do this. I place all images into assets folder. What do I have to change?
public void onClickWhatsApp() {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Hey, check out this cool game for Android ‘Free Ticket Bollywood Quiz’ www.globussoft.com";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Your title is about sharing image, but you try sharing text. Well. try any of these.
Try this to share text:
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "This is a test text");
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Try this to share image:
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
Uri uri=Uri.parse("file:///android_asset/myimage.png");
whatsappIntent.setType("image/*");
whatsappIntent.setPackage("com.whatsapp");
sendIntent.putExtra(Intent.EXTRA_STREAM,uri);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
Hope it helps.