Sharing images from one app to Whatsapp - java

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.

Related

FileUriEXposedException in Sharing screenshot of a layout

Hey i am trying to share the screen shot of a layout. it works very well in all very version which are less than Oreo, but when i am trying to share on Oreo it is giving FileUriExposedException. Please If anyone knows about this issue please solve it as soon as possible.
Here is my code
public Bitmap takeScreenshot() {
View rootView = (FrameLayout)findViewById(R.id.frame_layout).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
imagepath1 = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagepath1);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
private void shareIt() {
try{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
Uri uri = Uri.fromFile(imagepath1);
sharingIntent.setType("image/*");
String share_text = "*Create, Share, Download* Valentine Frames and spread love on this \nValentine\uD83D\uDE18\uD83D\uDE18 for free...." +
"\n*Download Now:-* https://play.google.com/store/apps/details?id=technoapps4.valentineframes2019";
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, share_text);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(sharingIntent, "Share via"));}
catch ( Exception e)
{
Toast.makeText(this, ""+e, Toast.LENGTH_SHORT).show();
}
AFAIK sharing files by sharing their paths is not allowed anymore since oreo. I got this issue some time ago.
My solution was to share the file content with a content-provider.

How to make a feature to share content in social media?

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)));

Android share via Intent not working,

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();
}

How to send Whatsapp message to new number

I' d like to send a whatsapp message by clicking on a button to a number that comes from the Android Activity (that in turn fetches from a server).
The number to which I have to send a new is NOT an existing contact on my phone.
I know how to open Whatsapp app from my app.
The following piece of code deals with opening whatsapp from an Adapter:
Intent sendIntent = new Intent();
sendIntent.setPackage("com.whatsapp");
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
this code opens Whatsapp but I don't know how to pass it the number to which I have to send the message
Try this
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.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
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}

Using Telegram to send a message

I'm trying to send a message to a telegram-app user, but the intent opens only the telegram app - it don't choose a conctact and send the message:
public void shareTelegram(String message)
{
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
waIntent.setPackage("org.telegram.messenger");
if (waIntent != null)
{
waIntent.putExtra(Intent.EXTRA_TEXT, message);//
startActivity(Intent.createChooser(waIntent, "Daniel"));
}
else
{
Toast.makeText(getApplicationContext(), "Telegram is not installed", Toast.LENGTH_SHORT).show();
}
}
Is there a way to send the message completely?
Can I send the message completely without displaying telegram ?
TLSharp is basic implementation of Telegram API on C#. See it here https://github.com/sochix/TLSharp
Try this.
try {
Toast.makeText(getApplicationContext(), "Sharing Via telegram !", Toast.LENGTH_LONG).show();
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("image/*");
waIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);//Check if package exists or not. If not then code
waIntent.setPackage("org.telegram"); //package check whether telegram is installed
waIntent.putExtra(Intent.EXTRA_TEXT, txt.getText().toString());//place your text here
startActivity(Intent.createChooser(waIntent, "Share with"));
}
catch (PackageManager.NameNotFoundException e)
{
Toast.makeText(SingleItemView.this, "telegram not installed", Toast.LENGTH_SHORT).show();
}

Categories

Resources