How to attach multiple files in an email with Android - android

I need attach several jpg files on gmail (android). I´m using ACTION_SENDMULTIPLE, but always receiving the error "all apps associated with this action have been disabled blocked or not installed".
I found many posts (several here) saying that this is very easy. You just need set the intent to ACTION_SENDMULTIPLE and put all files in a Uri.
Well, none worked in my case.
All this posts are relative old. I did'nt found posts after 2015.
I´m testing app in a Samsung S5 with android 6.0.1.
Any help ?
My code:
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); // it's not ACTION_SEND
//emailIntent.setType("text/rfc822");
emailIntent.setType("image/*");
emailIntent.setData(Uri.parse("mailto:" + Globais.wbEmailCorporativo)); // or just "mailto:" for blank
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Inventario Holdings = " + wData);
emailIntent.putExtra(Intent.EXTRA_TEXT, "Inventário.");
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this will make such that when user returns to your app, your app is displayed, instead of the email app.
//Attaching Files
ArrayList<Uri> uris = new ArrayList<Uri>();
File foto;
Uri u1 = Uri.fromFile(file);
uris.add(u1);
for (int a = 0; a < Globais.wbFotosTotal; a++) {
foto = new File(Globais.wbFotos[a]);
foto.setReadable(true, false);
Uri u2 = Uri.fromFile(foto);
uris.add(u2);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
try {
startActivity(emailIntent);
//startActivity(Intent.createChooser(email_intent, "Oi"));
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Erro:" + e.toString(), Toast.LENGTH_LONG).show();
return;
}

Related

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.

How to share a location to a Whatsapp contact?

I want to share my location to a Whatsapp contact.
I don't know what is the mimeType that I have to use. This is the code I'm usign to share:
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setPackage("com.whatsapp");
waIntent.setType("text/plain");
waIntent.putExtra(Intent.EXTRA_TEXT, "geo:23.1097,-82.4094");
startActivity(waIntent);
But this only send a plain text, not a location like Whatsapp does. Any idea??
Note: I donot have (and dont want to have) Whatsapp but maybe my findings help you
since "geo:..." is a uri (defined here) you should wrap it into a Uri and send it as data.
This code works with other location aware android apps like GoogleMaps
String uriString = "geo:23.1097,-82.4094";
Intent waIntent = new Intent();
// waIntent.setPackage("com.whatsapp");
Uri uri = Uri.parse(uriString);
waIntent.setData(uri); // finds several apps on my phone including googleMaps
// waIntent.setDataAndType(uri, "*/*"); // does not work on my phone: nothing found
Toast.makeText(this, appName + "Starting " + uriString, Toast.LENGTH_SHORT).show();
try {
this.startActivity(Intent.createChooser(waIntent,"Choose app to show location"));
} catch (Exception e) {
Toast.makeText(this, appName + e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
You can check if whatsapp understand this, too

File sharing using Intent.ACTION_SEND gives access denied on BBM

I am trying to share an audio file saved by my application. The file is added to the media store so all apps can access it.
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(finalFile);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
I use Intent.ACTION_SEND to share the files to other apps:
public void shareRecording(View view) {
Intent i = new Intent(Intent.ACTION_SEND);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.setType("audio/mp3");
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///" + recording.getFilePath()));
try {
startActivity(Intent.createChooser(i, "Share " + recording.getName()));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "There are no app installed to share your audio file.", Toast.LENGTH_SHORT).show();
}
}
All working good with all apps like GMail, Yahoo mail, Whatsapp, ...
except BBM, it gives access denied.
What do I need to do to make it work on BBM ?
Thanks for any help.
UPDATE:
I used
Uri fileUri = Uri.parse("content://" + recording.getFilePath());
instead of
Uri fileUri = Uri.parse("file:///" + recording.getFilePath());
and it worked on BBM but not othe apps
So what is the difference between havin the URI parsed with: "file:///" and "content://" ?
And how can I used this to get the share working for all apps ?
The solution is to use the actual file to initialize the Uri object.
File recordingFile = new File(recording.getFilePath());
Uri fileUri = Uri.fromFile(recordingFile);
This worked with all the apps that can share a file.

Android Attachments are not sent by email

I'm developing and android app, and at certain point i want to allow the user to send a .csv file by email.
I've seen a lot of tutorials in the web, and all of them have the same code, the same code i've tryed many times. The thing is, the send email activity starts, the attachment appears in the email, but when it is sent, the file or goes empty to the destination, or doesn't appear in the destination at all.
Here is some piece of code:
File file = null;
File dir = ProjectViewerResume.this.getCacheDir();
if (dir.canWrite()){
file = new File(dir, ProjectViewerResume.this.mProject.getName()+".csv");
FileOutputStream out = new FileOutputStream(file);
out.write(csv.toString().getBytes());
out.close();
}
Uri u1 = null;
u1 = Uri.fromFile(file);
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Project Resume: "+
ProjectViewerResume.this.mProject.getName());
sendIntent.putExtra(Intent.EXTRA_STREAM, u1);
sendIntent.setType("text/csv");
startActivity(sendIntent);
The csv variable has file content in csv format.
I've omited the try..catch so the code seems cleaner..
Thks in advance
You can try this....
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject of Email");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("/mnt/sdcard/file.csv"));
sendIntent.putExtra(Intent.EXTRA_TEXT, "Enjoy the mail");
startActivity(Intent.createChooser(sendIntent, "Email:"));
If some problem occur in attaching file try following path.
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/file.csv"));
I have the exact same problem. So not to duplicate the same post I want to show you my code, which is slightly different from yours, but still sends an email with a 0KB attachment.
I print 2 messages to the Log:
The path is printed (I edited the package name)
The size of the file is not 0KB !!!
05-11 11:32:58.133: W/Log Viewer(432): Path is /data/data/<package>/files/LogCat.log.gzip
05-11 11:32:58.192: W/Log Viewer(432): The file is 504 bytes
I stopped inside the code with the Eclipse debugger and inspected the uriLog: it is not null and contains the right path.
The code is:
public void run() {
Uri uriLog = null;
try {
FileOutputStream out = openFileOutput("LogCat.log.gzip", Context.MODE_PRIVATE);
out.write(gzip(dump().getBytes()));
out.close();
File gzipFile = new File(getFilesDir(), "LogCat.log.gzip");
//uriLog = Uri.parse("file://" + getFilesDir() + "/LogCat.log.gzip");
uriLog = Uri.fromFile(gzipFile);
Log.w(TAG,"Path is " + uriLog.getPath());
Log.w(TAG,"The file is " + gzipFile.length() + " bytes");
}
catch (IOException e) {
e.printStackTrace();
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Android Log");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, dump());
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uriLog);
try {
String str = getResources().getString(R.string.send_prompt);
Intent chooser = Intent.createChooser(emailIntent, str);
startActivity(chooser);
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
Things I tried:
I created the uri by parse (see the commented line
I also put the data inside the email body to check it exists --> it arrives in the email
I also tried to put the gziped data in the email body to check it exists --> it arrived in the email
EDIT:
Tried to send a pre-existing file in the emulator, from /mnt/system/app/CustomLocale.apk 23745 bytes
The path from uriLog.getPath() was printed /apk/CustomLocale.apk and the mail still arrived with an attachment named CustomLocale.apk and 0KB size
The only thing it occurs to me that is failing, is that putextra(intent.EXTRA_STREAM,uri) is based on
public Intent putExtra (String name, Parcelable value)
and uris implement the parcelable interface. But for some reason, which I will investigate, maybe the call to writeToParcel(Parcel out, int flags) is not writing the file data out; that's why it is 0KB in the attachment.
This code works now for me.
Using createTempFile instead of openFileOutput. I still don't know what was wrong with the former approach.
It attaches a file named LogCat.log.gzip which is not empty this time and it has the data I put insde.
public void run() {
Uri uriLog = null;
try {
File gzipFile = File.createTempFile("LogCat.log", ".gzip");
FileOutputStream out = new FileOutputStream(gzipFile);
out.write(gzip(dump().getBytes()));
out.close();
uriLog = Uri.fromFile(gzipFile);
}
catch (IOException e) {
e.printStackTrace();
}
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Android Log");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, dump());
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, uriLog);
try {
String str = getResources().getString(R.string.send_prompt);
Intent chooser = Intent.createChooser(emailIntent, str);
startActivity(chooser);
}
catch (Exception e) {
e.printStackTrace();
}
}
}).start();
And don't forget to add this line to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Android: ACTION_SEND_MULTIPLE with com.android.email

I'm trying to send multiple attachments in an Intent to the Email app (not the Gmail app). I'm using:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { "sample#email.com" });
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"This is an email");
emailIntent.putExtra(Intent.EXTRA_TEXT, "This is the body");
File f1 = null;
File f2 = null;
try {
f1 = new File("/sdcard/test");
f2 = new File("/sdcard/test.1");
FileWriter fw1 = new FileWriter(f1);
FileWriter fw2 = new FileWriter(f2);
fw1.write("this is some text");
fw2.write("this is more text");
fw1.close();
fw2.close();
} catch (IOException e) {
e.printStackTrace();
}
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(f1));
uris.add(Uri.fromFile(f2));
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,uris);
startActivity(emailIntent);
When Gmail is used to handle the Intent, it comes up with both attachments showing, and everything works just fine. When the Email app is used instead, no attachments are added. When using a single Uri in EXTRA_STREAM, the single attachment works, but using an ArrayList does not. I've pieced together this code from other questions asked on here, but none of them resolve this issue. Can anyone help?
use
emailIntent.setType(" */ * ");
with no spaces
see here ACTION_SEND_MULTIPLE
I realize that this is quite late, but your intent type is backwards. It should be
emailIntent.setType("text/plain");
not
emailIntent.setType("plain/text");
I'm surprised neither of the other answers pointed that out...
Instead of
emailIntent.setType("plain/text");
use
emailIntent.setType("application/octet-stream");
I don't know why, but it's working for me.

Categories

Resources