I developing small android application. In that user want to take backup of SQLite database so i try to attach the SQLite database to mail, when i run this app on android device, a dialog open n displaying Gmail & some other app. When I choose Gmail it automatically displaying send mail id, subject, email content and attachment also, now I click send mail was sent to corresponding email. When I check that Email attachment is not available their ?? Why SQLite Database didn't attached ? what is wrong with my code ?
File getdbdatapath= Environment.getDataDirectory();
String currentDBPath = "/data/com.example.appname/databases/dbname/";
File path=new File(getdbdatapath, currentDBPath);
Log.d("Path", path.toString());
//File extdb= Environment.getExternalStorageDirectory();
//String extpath=extdb+"/NewFolder/dbname";
//File pathp=new File(extdb, extpath);
//Log.d("New Path", pathp.toString());
Log.i(getClass().getSimpleName(), "send task - start");
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String address = "clientmailid#gmail.com";
String subject = "Application Database";
String emailtext = "Please check the database as attachement";
//emailIntent.setType("Application/database");
//emailIntent.setType("text/html");
//emailIntent.setType("message/rfc822");
emailIntent.setType("vnd.android.cursor.dir/email");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { address });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, emailtext);
startActivity(Intent.createChooser(emailIntent, "Send Mail..."));
I referred this link1 link2 link3, changed mailIntent.setType("text/html"); to many setTypes. Get database from external also, but didn't work. Help me. Thanks in advance.
This code is working fine
String extpath=Environment.getExternalStorageDirectory() +"/NewFolder/dbname";
File pathp=new File(extpath);
in my above code it take mnt>sdcard>mnt>sdcard>NewFloder>dbname so unable to attach now this code is working fine.
you need to copy your file to a place where the email activity can access it. E.g. to sdcard.
Related
I am trying to send one or more CSV files in my android application. The code works fine and when the gmail app is running, the attachment and all "EXTRAS" like subject, text are available. When I send the email, the email will arrive without attachment.
The e-mail will only be sent correctly (with attachment) if I change at least one letter in the email text/body.
If I remove the EXTRA_TEXT it's the same problem, I have to enter at least one letter manuel in the GMail app.
How you can see at the code, I also tried intent.setType("plain/text")
Here is my code:
File filePath = getActivity().getApplicationContext().getFilesDir();
File files[] = filePath.listFiles();
uris = new ArrayList<>();
for (File file : files){
if(file.getPath().endsWith(".csv")){
uris.add(FileProvider.getUriForFile(getActivity(),"com.my.company", file ));
}
}
if(uris.size() > 0){
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
//intent.setType("plain/text");
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"myEmail#abc.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "new CSV");
intent.putExtra(Intent.EXTRA_TEXT, "Hello, there is a new CSV file for you");
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(intent, "Sending multiple attachments"), REQUEST_EMAIL_CODE);
} else {
Toast.makeText(getActivity(), "no files available", Toast.LENGTH_SHORT).show();
}
Thank you for your help
I have created the backup file for the SQLite Database i have used in my application. All i want is to send this backup file through email. I have implemented the file sending Intent but when they open, it says, you can only send files like (Image, Coarse Location etc.)
String pathname = Environment.getExternalStorageDirectory().getAbsolutePath();
String filename = "/Android/data/<package-name>/databases/hello.db";
File file=new File(pathname, filename);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, "Database Backup");
i.putExtra(Intent.EXTRA_TEXT, "Hey there, database successfully sent.");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));`i.setType("text/plain");
startActivity(Intent.createChooser(i, "Your email id"));
The problem is due to Security reason, you can't email data from data folder, SO before emailing export it to SD card and then send, It should work.
Happy Coding !!!
Setting text/plain as attachment type probably causes email client to attempt to preview file as text. You should use other MIME type, such as "application/octet-stream", or even "any" mask - "*/*".
Here is an extremely dirty and simple snippet, that works just fine for me:
File dbFile = new File(Environment.getExternalStorageDirectory(), "example.db");
SQLiteDatabase.openOrCreateDatabase(dbFile, null);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "example text");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "database sending example");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dbFile));
startActivity(shareIntent);
I would like to attach string as html attachment in android mail,how can I achieve this.Kindly help me with a snippet to do this.Thanks a lot.
Use intent to send email like below way .. and if u want to add some string as attachment u have to save that content into some directory with .html ext and later u can add it as attachment into email intent. [This code for idea it may need few modification as per ur app requirement..]
String emailText = "email message";
final Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.setType("message/rfc822");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] { "demo#gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Hi");
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
String attachContent = "<html> content </html>";
/* this method u have to write, to save the string into html file
into cache or any other dir */
saveFile(getCacheDir()+"/attach.html", attachContent );
File file = new File(root, getCacheWebDir()+"/attach.html");
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
this.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
i want to send a .doc file as an attachment via email.
i am using the code below:
final Intent emailIntent = new Intent(
android.content.Intent.ACTION_SEND);
emailIntent.setType("plain/text");
emailIntent.putExtra(
android.content.Intent.EXTRA_SUBJECT,
"Song Pad.");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
"Please see the attached document.");
final File file = new File(dirPath + "/" + filename
+ ".doc");
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + file.getAbsolutePath()));
MainActivity.this.startActivity(Intent.createChooser(
emailIntent, "Send Email..."));
the attachment goes successfully to email but when i open it on device it shows message
Unable to find viewer for plain/text.
on the other hand when i send doc file not via my app then it opens perfectly.
please guide . i think there is little mistake..
thanks..
You should use a mime type of .doc documents application/msword.
Try:
emailIntent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(file);
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...