attach image from an app to gmail and share - android

I want to share my image from app only using gmail.
Here is my piece of code:
if (id == R.id.menu_item_share) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "compose mail");
sharingIntent.putExtra(Intent.EXTRA_STREAM, targetUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
How do I modify my codes

This method allow you to share the data for a single application.
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_EMAIL,`enter code here`
new String[] { "bipush.osti#gmail.com" });
share.putExtra(Intent.EXTRA_SUBJECT, "subject");
share.putExtra(Intent.EXTRA_TEXT, "compose mail");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
For example you can share the image for the gmail by using.
File filePath = new File("your image path");
share("gmail",filePath.toString(),"your comment");
you can also share to other application by simply changing the name.
share("facebook",filePath.toString(),"your comment");
share("twitter",filePath.toString(),"your comment");

Try to setType message/rfc822 instead of image/png

You have to mention the package name of the application which you want to handle it. To send it through the Gmail app add the below line
sharingIntent.setPackage("com.google.android.gm");

Related

Android share intent Twitter: How to share only by Tweet or Direct Message?

I need to share an image via only Tweet.
Here is my code
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
if (mInsertableToGallery) {
mInsertableToGallery = false;
mInsertedImagePath = MediaStore.Images.Media.insertImage(getContentResolver(), mShareImage,
getResources().getString(R.string.app_name) + System.currentTimeMillis(), getResources().getString(R.string.app_name));
}
// share only 5 specific apps:
List<Intent> targetedShareIntents = new ArrayList<>();
List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resolveInfo : resInfos) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
targetedShareIntent.setType("image/*");
targetedShareIntent.setPackage(packageName);
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(mInsertedImagePath));
if (TextUtils.equals(packageName, "com.facebook.katana") //fb
||TextUtils.equals(packageName, "com.instagram.android") //instagram
||TextUtils.equals(packageName, "jp.naver.line.android") //line
||TextUtils.equals(packageName, "com.google.android.apps.plus") // plus
||TextUtils.equals(packageName, "com.twitter.android")) // twitter
{
targetedShareIntents.add(targetedShareIntent);
}
}
}
Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "");
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooser);
and I get the result:
When click "Android System", Twitter share popup:
What I want:
How to share via Twitter only by Tweet or Direct Message ?
How to change title & icon of share item. Ex Twitter share item from "Android System" to "Tweet"?
I couldn't manage to get both Tweet and Direct Message to the front but the following code will get you either one there. Actually you can get both to the front but both appears with same name "Tweet" so its kind of unusable.
I have also included facebook, insta and tumblr.
To get Direct Message in place of tweet replace com.twitter.android.composer.ComposerActivity with com.twitter.android.DMActivity. To get both add another ' if ' for DMActivity.
Can't say surely but I don't think you can change the Icon of the package for your app.
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");//("text/plain");
Uri uri = Uri.fromFile(outFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo resolveInfo : resInfo) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("image/jpeg");
targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
if(TextUtils.equals(packageName,"com.facebook.katana")|TextUtils.equals(packageName,"com.instagram.android")
|TextUtils.equals(packageName,"com.tumblr")) {
targetedShareIntent.setPackage(packageName);
targetedShareIntents.add(targetedShareIntent);
}
if(TextUtils.equals(resolveInfo.activityInfo.name,"com.twitter.android.composer.ComposerActivity")) {
targetedShareIntent.setPackage(packageName);
targetedShareIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name);
targetedShareIntents.add(targetedShareIntent);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
For composing new Tweet:
public static Intent getTwitterShareIntent(Context context) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setClassName("com.twitter.android",
"com.twitter.composer.ComposerShareActivity");
return shareIntent;
}

android Share Intent not works for text and image both

Hi there is the code i used to share text and image :
Toast.makeText(App.getContext(), "Share", Toast.LENGTH_LONG).show();
Intent sharingIntent = null;
if(!image_resource.isEmpty()){
sharingIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
String imagePath = "SD-Card Path";
ExceptionHelpers.dLog("SHARE_LOG", "Share image path : " + imagePath);
ExceptionHelpers.dLog("SHARE_LOG", "Share image exist : " + (new File(imagePath).exists())); // It return 'true' on LogCat
if(new File(imagePath).exists()) {
ExceptionHelpers.dLog("SHARE_LOG", "Share image and text");
Uri imageUri = Uri.fromFile(new File(imagePath));
sharingIntent.setType("*/*"); // also 'image/*' tested and not works
sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
}else{
ExceptionHelpers.dLog("SHARE_LOG", "Share text");
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
}
}else {
ExceptionHelpers.dLog("SHARE_LOG", "Share text");
sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
}
String shareBody = ""+App.getString(R.string.this_text_shared_from)+" : "+App.getString(R.string.app_name)+" "+App.getString(R.string.share_text_2)+" \n "+
App.getString(R.string.share_about);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "" + App.getString(R.string.app_name));
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "" + App.getString(R.string.share_via)));
App is my custom class and App.getString is :
public static String getString(int resId){
return App.context.getString(resId);
}
it don't works well on Telegram, Gmail and same Apps And only share text with out Image
i found the answer for it, it work for me and i sent text and image both in one message to Telegram app.here is my code.
Intent intent = new Intent(Intent.ACTION_SEND);
packageName="org.telegram.messenger";
intent.setPackage(packageName);
//this is my art....
intent.setType("image/text/plain");
path=getBitMapPath();//this is path of image
uri = Uri.parse(path);
putExtra(Intent.EXTRA_STREAM, uri);
putExtra(Intent.EXTRA_TEXT, "hello telegram. i do it!");
ss = Intent.createChooser(intent, "hamid");
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ss);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
else
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
sharingIntent.setType("image/*");
sharingIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sharingIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getActivity().startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));

Sending text and Image simultaneously in Android

In my application, My requirement is to send Image and text simultaneously. So I use the following code
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));
startActivity(Intent.createChooser(share, "Share Image"));
But only the image is sended but the text is not sending. How can I solve this problem?
plz try this
//assuming uris is a list of Uri
Intent intent = null;
if (uris.size > 1){
intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
} else if (uris.size() == 1) {
intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, uris.get(0));}
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_TEXT, "Some message");
startActivity(Intent.createChooser(intent,"compatible apps:"));
You are setting the MIME type of that Intent to image that's why only the image is sent.
Something like this will solve your problem:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("*/*");
share.putExtra(Intent.EXTRA_TEXT, "My photos");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+f));
startActivity(Intent.createChooser(share, "Share Image"));
String message= "My photos";
URI = Uri.parse("file://" + f);
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("*/*");
if (URI != null) {
share.putExtra(Intent.EXTRA_STREAM, URI);
}
share.putExtra(android.content.Intent.EXTRA_TEXT, message);
startActivity(Intent.createChooser(share, "Share Image"));
This way should be ok.

Sharing through email only in android using Intent

I want to send the photo by only email using Intent. I am using below code but its not opening only gmail but showing many share options.
Please help me to share the only gmail.
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg"); // put here your mime type
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if(!resInfo.isEmpty()) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
ArrayList<Uri> uris = new ArrayList<Uri>();
for (ResolveInfo info : resInfo) {
if(info.activityInfo.packageName.toLowerCase().contains("gmail") || info.activityInfo.name.toLowerCase().contains("gmail")) {
targetedShare.setType("image/jpeg"); // put here your mime type
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Amplimesh Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,"Attached the Quote");
//Fetching the Installed App and open the Gmail App.
for(int index = 0; index < productList.size(); index++) {
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(productList.get(index).getOverlayBitmap());
Bitmap overLayBitmap = BitmapFactory.decodeStream(byteInputStream);
String fileName = SystemClock.currentThreadTimeMillis() + ".png";
//Save the bitmap to cache.
boolean isSaved = Helper.saveImageToExternalStorage(overLayBitmap, getApplicationContext(), fileName);
if(isSaved)
uris.add(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/amplimesh/images/" + fileName)));
}
}
}
targetedShare.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(targetedShare, "Sending multiple attachment"), 12345);
}
You can not get only gmail. but you can target some content type application.
try this
intent.setType("message/rfc822");
Try this:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("image/jpeg");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {""});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, EMAIL_SUBJECT);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, EMAIL_BODY);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+fileName));
startActivity(Intent.createChooser(emailIntent, "Sharing Options"));
For me its working..
try Intent.ACTION_SENDTO
This is the method.
public void emailShare()
{
Intent emailIntent = new Intent(android.content.Intent.ACTION_SENDTO);
emailIntent.setType("image/jpeg");
//File bitmapFile = new File(Environment.getExternalStorageDirectory()+"DCIM/Camera/img.jpg");
//myUri = Uri.fromFile(bitmapFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/DCIM/Camera/img.jpg"));
emailIntent.setData(Uri.parse("mailto:"));
startActivityForResult(Intent.createChooser(emailIntent, "Complete action using:"),PICK_CONTACT_REQUEST);
}
Where startActivityForResult is to get the result back. and MESSAGE_RESULT is the result expected if mail sent successfully.
Catch the result on
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == MESSAGE_RESULT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "E-Mail sent successfully", Toast.LENGTH_LONG).show();
}
}
}
Declare static final int MESSAGE_RESULT = 1; at the begining.
Hope it helps.
use Intent.ACTION_VIEW insted of Intent.ACTION_SEND
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("mailto:?subject=" + "Subject" + "&body=" + "Body" + "&to=" + "email#mail.com");
intent.setData(data);
startActivity(Intent.createChooser(intent, "Choose app"));
or you can use:
startActivity(intent);
String shareImageLocation="Image file address";//Give file address here
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "someone#someone.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Send photos");
i.putExtra(Intent.EXTRA_STREAM, shareImageLocation);
String bugReportBody = description;
i.putExtra(Intent.EXTRA_TEXT, bugReportBody);
ArrayList<Uri> uris = new ArrayList<Uri>();
File fileIn = new File(shareImageLocation);
uris.add(Uri.fromFile(fileIn));
i.putExtra(Intent.EXTRA_STREAM, uris);
try {
startActivityForResult(
Intent.createChooser(i, "Complete action using"),
SEND_EMAIL);
} catch (android.content.ActivityNotFoundException ex) {
}
Using only gmail app is not a good idea. Because there lot of phones which doesn't have gmail app installed. Try this code which will show all the app which can send email. I'm sure it won't show all the sharing app in your phone. But it will show some others apps which can handle any sharing intent. Like google drive.

Sharing on Twitter and Facebook via android app

I'm developing an adroid app with wich I can share text (and later pictures too) via facebook and twitter. I found some code which is opening the facebook/twitter sharing window but the text that needs to be shared is in an EditText in the app so my question is how can i insert my text from the EditText to te text that will be shared (sorry for the bad english if you need further explanation or you didn't understood anything then i will try too explain it better). This is my code for both sharing methods:
Facebook:
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
(String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
(String) v.getTag(R.drawable.ic_launcher));
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(
shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Twitter:
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
(String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
(String) v.getTag(R.drawable.ic_launcher));
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ("com.twitter.android.PostActivity"
.equals(app.activityInfo.name)) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Use this method to share photo with text. Call this method and pass argument nameofapp and imagepath. Name of app means like on which you want to share like gmail , facebook , twitter.
private void share(String nameApp, String imagePath,String text) {
try
{
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
targetedShare.putExtra(Intent.EXTRA_TEXT,text);
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
catch(Exception e){
}
}
Use this for facebook :-
try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("facebook",filePath.toString(),"Hello"); <<<<<Hello is text. send acc. to you req.
}
catch(Exception e) {
//exception occur might your app like gmail , facebook etc not installed or not working correctly.
}
For twitter
try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("twitter",filePath.toString(),"Hello"); <<<<<Hello is a text send acc. to you req.
}
catch(Exception e) {
//exception occur might your app like gmail , facebook etc not installed or not working correctly.
}
For attaching image on gmail,facebook,twitter with text use below code.
File filePath = context.getFileStreamPath("vmphoto.jpg");
share("gmail",filePath.toString());
share("facebook",filePath.toString());
share("twitter",filePath.toString());
// code of share(String nameApp,String imagePath) function
void share(String nameApp, String imagePath) {
try
{
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()){
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by App Name");
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
catch(Exception e){
Log.v("VM","Exception while sending image on" + nameApp + " "+ e.getMessage());
}
}

Categories

Resources