email attachment only works with changed extra_text - android

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

Related

Android: Only show email apps OR display a message to user for Intent.ACTION_SEND when attaching a file

Right now for sdk 30, I am successfully able to send and attach a json file to some email client when using ACTION_SEND. However there are two issues that I noticed when using ACTION_SEND:
it shows ALL apps, not just email apps, which is what I want
it does NOT show the intent with title/text for the user to choose an email app. I do NOT just want to use a Toast message
I think this can be solved using ACTION_SENDTO instead, but then I won't be able to attach my file, so that actually won't work.
With this said, I would like to solve either point 1 or 2 (preferably point 1), if not both.
Here is what I have:
private void sendToEmail(String emailAddress, String filePath) {
File file = new File(filePath);
file.setReadOnly();
String[] to = { emailAddress };
Uri uri = FileProvider.getUriForFile(this, getPackageName() + ".fileprovider", file);
grantUriPermission("com.my.package", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = new Intent(Intent.ACTION_SEND);
//intent.setData(Uri.parse("mailto:")); // this does NOT work for the above action type
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_SUBJECT, "Your backup file");
intent.putExtra(Intent.EXTRA_TEXT, "Your data for your backup is attached to the file " + BACKUP_NAME + ".");
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("message/rfc822");
// this does NOT create the below title for ACTION_SEND, as per documentation for createChooser()
Intent chooser = Intent.createChooser(intent, "Pick an email app to send attachment: " + BACKUP_NAME);
if (chooser.resolveActivity(getPackageManager()) != null) {
startActivity(chooser);
}
}

Stray file path added to email on intent using FileProvider attachment

I've got an intent that uses FileProvider to read from internal file storage for my app to send a file via email (or similar apps). The code works great everywhere apparently except for Gmail, which strangely adds a version of the provider path itself to the list of addressees of the email.
This is the code generating the intent:
public static Intent getSendIntent(Uri uri) {
final Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setDataAndType(uri, "message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "my#email.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "Export");
i.putExtra(Intent.EXTRA_TEXT, "See the attached...");
i.putExtra(Intent.EXTRA_STREAM, uri);
return i;
}
This is the code to start the activity:
File file = new File(getFilesDir(), filename);
Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.myapplication.provider", file);
Intent i = Utils.getSendIntent(uri);
try {
startActivity(Intent.createChooser(i, "Send file..."));
} catch (Exception e) {
Log.d(TAG, "failed to start send activity: " + e.getMessage());
Toast.makeText(MainActivity.this, "No suitable activity found for export.", Toast.LENGTH_SHORT).show();
}
Works well in Slack, Evernote, etc. but in Gmail in addition to the email address I've provided, another addressee in this kind of format is added:
//com.example.myapplication.provider/my_files/filefile.csv
which prevents the email from sending until it's manually removed from the message. Everything else about the message is as expected.
Any clue how to prevent this?
The following Captain Obvious solution resolved the problem.
Replace:
i.setDataAndType(uri, "message/rfc822");
with:
i.setType("message/rfc822");
since, as #CommonsWare's question inferred, the file content isn't itself in rfc822 format.

Attachment is not attaching with email

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.

Android: Download File then show app chooser to open it in?

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..."));

Unable to send email with attachments from my app using intents (Gmail)

I'm trying to send files (.log files) contained in a sdcard's folder using Intent. This is the code:
public void sendMail() {
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {"name.surname#gmail.com"});
intent.putExtra(Intent.EXTRA_SUBJECT, "Log files");
intent.putExtra(Intent.EXTRA_TEXT, "body");
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
File root = Environment.getExternalStorageDirectory();
File logfolder = new File(root, "log");
for (String file : logfolder.list()){
Uri u = Uri.parse(file);
uris.add(u);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(intent, new String("Send mail...")));
}
}
I choose Gmail from menu. Once Gmail is opend it displays a mail with recipient, subject, text, and attachment files correctly. The mail is sended with no error but i get a status bar notification that says "couldn't show attachment"! In fact recipient receives email correctly but it has no attachment.
I'm not able to figure out what is the problem. Why attachments are not sended? Please help me!!
Ok. i find the solution. Need to replace this:
for (String file : logfolder.list()){
Uri u = Uri.parse(file);
uris.add(u);
}
with this:
for (File file : logfolder.listFiles()){
Uri u = Uri.fromFile(file);
uris.add(u);
}

Categories

Resources