I want to share a file made and written using Service via email. I know I can't share a private file with email but how to use the content provider to do that. I read online that content Provider can help but I can't make it work. (I merged file creation code together with intent creation for simplicity)
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{ "someone#gmail.com"});
email.putExtra(Intent.EXTRA_SUBJECT, "Nothing");
email.putExtra(Intent.EXTRA_TEXT, "Nothing");
email.setType("message/rfc822");
Uri uri = null;
try {
File file = new File(this.getExternalFilesDir(null), "samplefile.txt");
uri = FileProvider.getUriForFile(this, "lcukerd.com.android.fileprovider", file);
FileOutputStream osw = new FileOutputStream(file);
osw.write("Say something".getBytes("UTF-8"));
osw.close();
Log.i("File Reading stuff", "success");
} catch (Exception e)
{
Log.e(tag,"File creation error",e);
}
//Uri uri = FileProvider.getUriForFile(this, "lcukerd.com.android.fileprovider", );
//Uri uri = Uri.fromFile(getFileStreamPath("samplefile.txt"));
Log.d(tag,uri.toString());
email.putExtra(Intent.EXTRA_STREAM,uri);
startActivityForResult(Intent.createChooser(email, "Choose an Email client :"),1);
I get option to choose app but then nothing happens. Gmail app does not open.
Gmail opens if I use Uri uri = Uri.fromFile(getFileStreamPath("samplefile.txt")); but then I get "permission denied for attachment".
Actually, I want to write file from service then send that file via email. Pls help me achieve that.
Use the following code,
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {"me#gmail.com"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Test Subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"go on read the emails");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromfile(new File(yourtextfilepath));
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Make sure that your text file path should be from external memory card. Action send wont accept the files from internal memory.
Also try this also,
Link 1
Related
I wish to implement a button that upon pressing it will open the default email client with an attachment file.
I am following this, but am getting an error message on the startActivity, saying it is expecting an activity param while I am giving it an intent.
I am using API 21 and Android Studio 1.1.0, so perhaps it has something to do with the comment in the answer provided in the link?
This is my fourth day as Android developer so sorry if I am missing something really basic.
Here is my code:
public void sendFileToEmail(File f){
String subject = "Lap times";
ArrayList<Uri> attachments = new ArrayList<Uri>();
attachments.add(Uri.fromFile(f));
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
intent.setClassName("com.android.email", "com.android.mail.compose.ComposeActivity");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
Official documentation with Kotlin snippets is here: https://developer.android.com/guide/components/intents-common#ComposeEmail
I think your problem is that you are not using the correct file path.
The following works for me:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email#example.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = "temp/attachement.xml";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
EDIT: Requesting access to storage just to share a file private to your app is probably not a good idea. Fortunately, after a little configuration, it's very easy to share a file from your app private storage. See this guide: https://developer.android.com/training/secure-file-sharing/setup-sharing
If you share a file that is on external storage, you also need to give the user permission via a manifest file like below
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
For newer devices you will encounter FileUriExposedException. Here is how to avoid it in Kotlin.
val file = File(Environment.getExternalStorageDirectory(), "this")
val authority = context.packageName + ".provider"
val uri = FileProvider.getUriForFile(context, authority, file)
val emailIntent = createEmailIntent(uri)
startActivity(Intent.createChooser(emailIntent, "Send email..."))
private fun createEmailIntent(attachmentUri: Uri): Intent {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "vnd.android.cursor.dir/email"
val to = arrayOf("some#email.com")
emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
return emailIntent
}
Try to use this.It is working...
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(listVideos.get(position).getVideoPath())));//path of video
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Thanks
I can get both Mail and Gmail to attach multiple csv files to an email.
When sent via Mail all the attachments are delivered.
When sent by Gmail none of the attachments are delivered.
I have read the documentation Send Binary Content. I have searched but only found a solution for Gmail that does not work with Mail. Mail seems happy with just about any approach. Gmail just doesn't want to play.
Has anyone found a solution for sending multiple attachments that works with both Mail and Gmail?
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
String subject = context.getString(R.string.export_data_email_header);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.setType("text/csv");
ArrayList<Uri> uris = new ArrayList<Uri>();
if (diariesSelected) uris.add(Uri.fromFile(context.getFileStreamPath("diaries.csv")));
...
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(emailIntent);
And the code used to create the file
FileOutputStream fos = context.openFileOutput(path, Context.MODE_WORLD_READABLE);
OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.append(builder.toString());
writer.close();
fos.close();
The following code is a snippet from one of my apps. As far as I remember, it works with GMail and Mail (Can't verify it at the moment.). It looks basically like your solution, but with a few little differences. Maybe one of them is what you are looking for. :)
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { "address#mail.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "The subject");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "The actual message");
ArrayList<Uri> attachmentUris = new ArrayList<Uri>();
for (File attachment : attachments) {
attachmentUris.add(Uri.fromFile(attachment));
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentUris);
startActivity(emailIntent);
Here you can get detail information https://stackoverflow.com/a/18225100/942224
by using below code i am attaching image file in gmail or Mail.... hope it will help you
Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("plain/text");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"email id"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
ArrayList<String> fileList = new ArrayList<String>();
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/certi/qualifications.jpg");
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/certi/certificate.jpg");
fileList.add(Environment.getExternalStorageDirectory()+"/foldername/Aa.pdf");
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (int i=0;i<fileList.size();i++)
{
File fileIn = new File(fileList.get(i));
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
What is the type i can use for intent for sending an email with an html page attachment? Whatever type i use, the sent mail contains an empty page as an attachment
Here's some code
String root = "/data/data/com.email/files/";
String fileName = "Payslip.html";
if (true) {
attachment = new File(root, fileName);
}
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("application/octet-stream");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[] { address.getText().toString() });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
subject.getText());
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
emailtext.getText());
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(attachment));
EmailActivity.this.startActivity(Intent.createChooser(
emailIntent,
"Send mail..."));
As for now, we can use context.getFilesDir() to retrieve a File object to the "files" directory. With that you can also get another File object to your stored HTML page. You can also use the File.exists() and File.isFile() method for checking. Thanks for Christian Lisching
I am sending mail through my application.
For that I am using following code.
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient#example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
It just work fine but i want to attach an xml file with it.
Is it possible? How?
There are lots of similar questions asked with perfect solution in Stack Overflow already.
You can have look to few : here and here and here
Solution is to use with email intent : one more putExtra with Key-Extra_Stream and Value-uri to file.
And please go through the FAQ to undersatand How to better benifit from the site.
String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();
String filename="/MyFiles/mysdfile.txt";
File file=new File(pathname, filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Title");
i.putExtra(Intent.EXTRA_TEXT, "Content");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Your email id"));
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);
For sending an attachment with gmail:
File should be on External storage device or created in External storage device
For that you need to add the following to Android Manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
get External path by
String pathname= Environment.getExternalStorageDirectory().getAbsolutePath();
Create a new file by
File myfile=new File(pathname,filename);
Write to file based on whatever logic you are applying
Now the Intent
Intent email=new Intent(android.content.Intent.ACTION_SEND);
email.setType("plain/text");
Put Extras
email.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(myfile)); email.putExtra(Intent.EXTRA_SUBJECT, "my email subject");
email.putExtra(Intent.EXTRA_TEXT, "my email text");
Start Activity
startActivity(Intent.createChooser(email, "E-mail"));
consider the code below:
String outFileName = "/data/data/com.packagename/attachment.ics";
emailintent.putExtra(Intent.EXTRA_STREAM, Uri.parse(outFileName));
emailintent.setType("plain/text");
startActivity(Intent.createChooser(emailintent, "Send mail..."));
The above code is starting the email client with the attachment shown when it starts. But when i send the email, the attachment is not received. The body is being received.
what is going wrong here?
thank you in advance.
EDIT:
Is there a specific mime type that i need to put for ics files? i even tried sending a txt file, but that too is not being sent. The attachment does show up when i am trying to send the email, but it does not appear when i receive the email
i found the problem that was occurring. I was putting the file that i want to attach to the email into a private folder inside my application. The email client was not able to access..
All i had to do was put it in a public directory on the sdcard and voila.. the email client got access and i started receiving in the mails i sent from my application.
PS: Even for ics files the MIME type is plain/text.
thanks for all your help.
There are a lot of threads related to this topic.
Did you try adding this
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentFilePath));?
How to send an attachment with the Email in android?
Android: How do I attach a temporary, generated image to an email?
problem sending an email with an attachment programmatically
I am facing the same problem in sending email with attach SQLite database. I am Searching for the solution for this problem but i found nothing.
there is no way to send attached file via email from internal storage.
For sending file via email first save that file to external storage and then attach and send.
I am using this code for saving file to sdcard
public void copyFileToSdCard()
{
File f = new File(Environment.getExternalStorageDirectory() + "/File name");
if(f.exists())
{
}
else
{
try
{
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite())
{
String currentPath = "file path";
String backupFilePath = "file name ";
File currentFile = new File(data, currentPath);
File backupFile = new File(sd, backupFilePath);
if (currentFile.exists()) {
FileChannel src = new FileInputStream(currentFile).getChannel();
FileChannel dst = new FileOutputStream(backupFile).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
}
catch (Exception e) {
Log.w("Backup", e);
}
}
}
and this for attach and send
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] {emailAddress});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
i.putExtra(Intent.EXTRA_TEXT, "Body text");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "file name"));
i.putExtra(Intent.EXTRA_STREAM, uri);
i.setType("text/plain");
startActivity(Intent.createChooser(i, "Send mail"));
try this
emailintent.setType("text/calendar");
public class SendingMail {
public static void SendingMail(Context context) {
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc#wxy.com"});
emailIntent.setType("text/html");
// Image file saved in sdcard
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+File.separator+"sdcard"
+ File.separator + "MyImage.png"));
emailIntent.putExtra(Intent.EXTRA_TEXT, "My image");
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
}
This will work...