What I've done so far is :
File root = Environment.getExternalStorageDirectory().getAbsoluteFile();
String pathToMyAttachedFile = "/myapp.vcf";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
Uri uri = Uri.fromFile(file);
Intent smsInbox = new Intent();
smsInbox.setAction(Intent.ACTION_SEND);
smsInbox.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
smsInbox.putExtra("address", phoneno);//For sending to specific number
smsInbox.putExtra(Intent.EXTRA_STREAM,uri);
//smsInbox.putExtra(Intent.EXTRA_TEXT,content);
smsInbox.putExtra("sms_body",content);
smsInbox.setType("text/x-vcard");
context.startActivity(Intent.createChooser(smsInbox,"send"));
If I do not add "address" key-value pair in intent then it would display a chooser asking to whom i want to send and then it works in all devices, like I'm able to send the text + VCard attachment. but I need to add address key as I want to send v-card with text to particular number but then it doesn't work.
I have been trying since long, I have tried all the solutions that I found on SO but nothing helped.
In my Application I try to share an Image with the Intent (Intent.ActionSend).
For the guaranteed Access I use a Fileprovider which creates the Uri to the file.
Under Android Nougat my code works but with Marshmallow not. It shows no errors but the Image is not sent to the recieving App.
The Output from VisualStudio also shows no errors or somthing like that.
I couldn't find any answer online so far. What is the problem with Marshmallow?
public void SendPhoto(IEnumerable<string> photos, string text)
{
var files = new List<Android.Net.Uri>();
//creates for every photo an own ContentURI
foreach (var path in photos)
{
var file = new Java.IO.File(path);
files.Add(FileProvider.GetUriForFile(Context, "com.testapp.fileprovider", file));
}
//Checks if Photolist are empty
if (!files.Any())
throw new Exception("Photos not found");
Intent intent = new Intent(Intent.ActionSend);
intent.SetFlags(ActivityFlags.GrantReadUriPermission);
intent.SetType("image/jpeg");
intent.PutExtra(Intent.ExtraSubject, "Subject");
intent.PutExtra(Intent.ExtraStream, files.First());
//creates the sharemessage
var shareMessage = files.Count == 1 ? "Send photo from TestApp" : "Send photos from TestApp";
//start Intent chooser
Context.StartActivity(Intent.CreateChooser(intent, shareMessage));
}
I have seen some other example for using intent.ACTION_SEND_MULTIPLE. But they are example
all write for e-mail. My question is that I have about ten files to send,and I use intent
to send it via Bluetooth,I can successfully send those files to other device but it will
appear the intent chooser ten times,I just want it to appear just once or I can assign the
intent using Bluetooth in advance to send files,with not pop up any chooser. Can anybody
give me some suggestion?
The following is my code:
ArrayList<File> fileList = new ArrayList<File>();
ArrayList<Uri> files = new ArrayList<Uri>();
StringTokenizer tokens = new StringTokenizer(options, ",");
String stored = "";
while (tokens.hasMoreTokens()) {
stored = tokens.nextToken();
File file = new File(stored);
fileList.add(file);
}
for(File file : fileList ) {
Uri uri = Uri.fromFile(file);
files.add(uri);
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND_MULTIPLE);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, files);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "select some pic"));
}
Thanks in advance for seeing my question!
I finally find the answer , I add the following code and it works perfect
If anyone face the problem , maybe you can have a try!
intent.setPackage("com.android.bluetooth");
It would select Bluetooth to share files only and won't appear any
unnecessary intent chooser !
I want to send a picture preview in my mail Intent.
I dont want to attach it, i just want it to be shown.
This is my intent:
String textToSend = getString(R.string.mailHi)+"<br><br>"+getString(R.string.mailText)+getTextToSendViaMail();
Uri pngUri = null;
File currentShopImage = new File(Environment.getExternalStorageDirectory().toString()+"/OpenGuide/"+Integer.toString(keyId)+"_1_normal.pn_");
if(currentShopImage.exists()){
File pngFile = new File(currentShopImage.toString());
pngUri = Uri.fromFile(pngFile);
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
//i.putExtra(Intent.EXTRA_EMAIL, new String[] { emailCim });
i.putExtra(Intent.EXTRA_SUBJECT, "OpenGuide");
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(textToSend));
if(pngUri!= null)
i.putExtra(Intent.EXTRA_STREAM, pngUri);
try {
startActivity(Intent.createChooser(i, getString(R.string.SendMail)));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ShopActivity.this, getString(R.string.MailClientNotFound), Toast.LENGTH_SHORT).show();
}
How can i achive such a thing ?
As far as I understood your problem, you want to place the image inside the mail with other text. In the words of Email protocol it's called an INLINE Attachment.
You would not be able to do this with intents as none of the email clients installed on the device supports creating html messages.
If it's really a core part of your app, you should consider a third party api to do this. One of the such library is JavaMail. You would be able to send html messages through this library but will take some time in setting up.
Here's a link that may give you some hint, how it's done.
Then you need to send a HTML generated email afaik.
I've been working on Android program to send email with an attachment (image file, audio file, etc) using Intent with ACTION_SEND. The program is working when email has a single attachment. I used Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is working fine, the mail can be delivered through the Gmail. However, when I tried to have multiple images attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.
I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constant ACTION_SEND_MULTIPLE (available since API level 4) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else, it works like ACTION_SEND, except the data is multiple. But I still could not figure out the correct usage for this command. I tried to declare intent with ACTION_SEND_MULTIPLE, then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images, but I got the same erroneous result just like before, none of the attachment show up in the email.
Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment?
Here is the code you need to create an emailIntent that contains multiple attachments.
public static void email(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths)
{
//need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{emailCC});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
ACTION_SEND_MULTIPLE should be the action
and then emailIntent.setType("text/plain");
followed by:
ArrayList<Uri> uris = new ArrayList<Uri>();
String[] filePaths = new String[] {"sdcard/sample.png", "sdcard/sample.png"};
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(emailIntent);
This works for me.
Although this is an old thread, but as it is shown on top on google searches i want to add a small hint to make it complete, hence I stumpled upon it.
It is necessary to make the attached files readable for the mail activity, otherwise they will not be attached. So you have to call somewhere
fileIn.setReadable(true, false)
Here I found great example http://www.blackmoonit.com/2010/02/filebrowser-send-receive-intents/
you must use
final Intent aIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
aIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,theUris);
aIntent.setType(theOverallMIMEtype);
For multiple attachments use PutParcelableArrayListExtra(Intent.ExtraStream, uris)where uris variable is a List<IParcelable>().
Here's an example:
var email = new Intent(Intent.ActionSendMultiple);
email.SetType("text/plain");
email.PutExtra(Intent.ExtraEmail, new string[]{emailTo});
email.PutExtra(Intent.ExtraCc, new string[]{emailCC});
var uris = new List<IParcelable>();
filePaths.ForEach(file=> {
var fileIn = new File(file);
var uri = Android.Net.Uri.FromFile(fileIn);
uris.Add(uri);
});
email.PutParcelableArrayListExtra(Intent.ExtraStream, uris);
context.StartActivity(Intent.CreateChooser(email, "Send mail..."));
Hope this helps ;)