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);
Related
//EMAIL SENDING CODE FROM ASSET FOLDER
email = editTextEmail.getText().toString();
subject = editTextSubject.getText().toString();
message = editTextMessage.getText().toString();
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("file/html");
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://com.example.deepa.xmlparsing/file:///android_assets/Combination-1.html"));
startActivity(Intent.createChooser(emailIntent, "Send email using"));
Finally, I'm getting the file from asset folder (Combination-1.html).
It is getting
Runtime error file not found exception.
Is there any other way to send file attachment?
The easiest way to send an email is to create an Intent of type ACTION_SEND :
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
intent.putExtra(Intent.EXTRA_TEXT, "Attachment test");
To attach a single file, add some extended data to Intent :
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/path/to/file")));
intent.setType("text/html");
Or use Html.fromHtml() to build html content:
intent.putExtra( Intent.EXTRA_TEXT,
Html.fromHtml(new StringBuilder()
.append("<h1>Heading 1</h1>")
.append("<p><a>http://www.google.com</a></p>")
.toString()));
For multiple attachments:
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/html");
intent.putExtra(Intent.EXTRA_SUBJECT, "Test multiple");
intent.putExtra(Intent.EXTRA_TEXT, "multiple attachments");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{recipient_address});
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(new File("/path/to/first/file")));
uris.add(Uri.fromFile(new File("/path/to/second/file")));
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
Finish with a call to startActivity() passing intent.
Create a File object of your asset folder file and attach this object to your email Intent.
And as mentioned in your Question runtime error file not found exception this may be cause the URL "file:///android_asset/" doesn't point to a particular directory, it is only used by WebView to address assets. Pulled that from
you can open this as an input stream and covert this InputStream to File
in = new BufferedReader(new InputStreamReader(activity.getAssets().open(myfile.pdf)));
Send this file object in email as follow.
Intent intent = new Intent(Intent.ACTION_SEND ,Uri.parse("mailto:"));
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Card Set ");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(file));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
Where Intent.ACTION_SEND used to send Email , Intent.EXTRA_STREAM for the attachments with email. you can have multiple Intent.EXTRA_STREAMin single intent to refer multiple attachments with intent.setAction(Intent.ACTION_SEND_MULTIPLE);.
intent.setType(String mimeType) input param is represent the MIME type data that you want to get in return from firing intent(here intent instance).where setype can be
image/jpeg
audio/mpeg4-generic
text/html
audio/mpeg
audio/aac
audio/wav
audio/ogg
audio/midi
audio/x-ms-wma
video/mp4
video/x-msvideo
video/x-ms-wmv
image/png
image/jpeg
image/gif
.xml ->text/xml
.txt -> text/plain
.cfg -> text/plain
.csv -> text/plain
.conf -> text/plain
.rc -> text/plain
.htm -> text/html
.html -> text/html
.pdf -> application/pdf
.apk -> application/vnd.android.package-archive
You can use following code to achieve your goal , and can change Hard coded string according your requirements .
String filename="contacts_sid.vcf";
File filelocation = new
File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// Here you can set the type to 'email'
emailIntent .setType("abc.xyz.cursor.dir/email");
String to[] = {"android#gmail.com"};
emailIntent .putExtra(Intent.EXTRA_EMAIL, to);
// For the attachment
emailIntent .putExtra(Intent.EXTRA_STREAM, path);
// the mail subject
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Find Attachments with email");
startActivity(Intent.createChooser(emailIntent , "Sending email..."));
You can not share Asset folder file to other application without ContentProvider. You can send file which can be accesible for every application.like some whare in intenal phone memory or sd card.
Sharing Asset Check This Answer of #CommonsWare
Or
Read your text file and use #RonTLV Answer and mail.
Or
Copy File in intenal memory and add to sending launch.
Hope this answer will help.
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.
So I've been looking all around and can't seem to figure it out, or maybe because I'm inside the emulator?
Basically I'm trying to download a file, and then show a app chooser so the user can freely choose which ever app to open it in.
One thing I'm not sure about is, how do you do a wild card mime type for the intent? I mean for example the downloaded file could be shared and opened by the mail client as an attachment, so really it should support anything.
For the purpose of brevity, the download code works and downloads, so assume the download is completed and the file is in the cache directory:
Intent install = new Intent(Intent.ACTION_VIEW);
// Create intent to show chooser
Intent chooser = Intent.createChooser(install, "Open in...");
// Verify the intent will resolve to at least one activity
if (install.resolveActivity(_progressDialog.getContext().getPackageManager()) != null) {
_progressDialog.getContext().startActivity(chooser);
}
Am I doing anything wrong?
I believe you need to provide the mime type as well e.g. with
intent.setDataAndType(Uri, mimetype);
where mimetype is like "text/plain". You can extract it from file extension, header using special method or lookup table (I don't recall how exactly it is done now).
But that's not working either at least in the emulator.
You can use this below intent to attach thefile & send email
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"email#example.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
intent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
File file = new File(root, xmlFilename);
if (!file.exists() || !file.canRead()) {
Toast.makeText(this, "Attachment Error", Toast.LENGTH_SHORT).show();
finish();
return;
}
Uri uri = Uri.parse("file://" + file);
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Send email..."));
In my app I want to send the history stored in my SQLite databse via email to another person,
(i.e as there is an option in 'Whatsapp' to send user chat via email, as a text file).
So here I just want an approach how to begin with this problem.
Thanks
You need to add EXTRA_STREAM to your Intent that sends e-mail:
intent.setType("application/octet-stream");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(databaseFile));
Where databaseFile is your database file.
Don't forget that you need to copy your file on external storage because you cannot attach file(s) stored in application-package directory.
Whole code can looks like:
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[] {"recipient#domain.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "Some message text");
i.setType("application/octet-stream");
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(databaseFile));
startActivity(Intent.createChooser(i, "Send e-mail"));
If you want to put multiple attachements, you can use:
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
...
ArrayList<Uri> items = new ArrayList<Uri>();
Uri uri = Uri.fromFile(new File(pathToFile));
items.add(uri);
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, items);
Note: Same will work for txt files.
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);