i'm using this code to able my users to share apps via bluetooth or other networks . It works fine , the only problem I've is this ,when the app file received on the other phone ,it gets the package name as file name , it don't want to use package name as file name .
this is the code :
try {
ArrayList<Uri> uris = new ArrayList<Uri>();
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("application/vnd.android.package-archive");
uris.add(Uri.fromFile(new File(getBaseContext().getApplicationInfo().publicSourceDir)));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, null));
} catch (Exception e) {
ArrayList<Uri> uris = new ArrayList<Uri>();
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("application/zip");
uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir)));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, null));
}
How can I use app name as file name when It sends to others phones?
I made similier type of app long ago, You have to Copy that apk file somewhere into your sdcard and then you can rename it. Send it to anyone with any kind of name.
Related
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'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);
}
I am succeeded to attach and send single file using mail Intent. But I cant attach two files, I have used the below code from some of the websites.
final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("plain/text");
File file1 = getApplicationContext().getFileStreamPath("file1.csv");
File file2 = getApplicationContext().getFileStreamPath("file2.csv");
ArrayList<Uri> uris = new ArrayList<Uri>();
uris.add(Uri.fromFile(file1));
uris.add(Uri.fromFile(file2));
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
I wrote the files in world_readable mode. But I dont know why I am not able to attach the files. Please give me your suggestion..
I've been working on Android program to send email with an attachment (image file, audio file, etc) using Intent with ACTION_SEND. The program is working when email has a single attachment. I used Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) to attach the designated image file to the mail and it is working fine, the mail can be delivered through the Gmail. However, when I tried to have multiple images attached to the same mail by calling Intent.putExtra(android.content.Intent.EXTRA_STREAM, uri) multiple times, it failed to work. None of the attachment show up in the email.
I searched the SDK documentation and Android programming user group about email attachment but cannot find any related info. However, I've discovered that there's another intent constant ACTION_SEND_MULTIPLE (available since API level 4) which might meet my requirement. Based on SDK documentation, it simply states that it deliver multiple data to someone else, it works like ACTION_SEND, except the data is multiple. But I still could not figure out the correct usage for this command. I tried to declare intent with ACTION_SEND_MULTIPLE, then call putExtra(EXTRA_STREAM, uri) multiple times to attach multiple images, but I got the same erroneous result just like before, none of the attachment show up in the email.
Has anyone tried with ACTION_SEND_MULTIPLE and got it working with multiple email attachment?
Here is the code you need to create an emailIntent that contains multiple attachments.
public static void email(Context context, String emailTo, String emailCC,
String subject, String emailText, List<String> filePaths)
{
//need to "send multiple" to get more than one attachment
final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
new String[]{emailTo});
emailIntent.putExtra(android.content.Intent.EXTRA_CC,
new String[]{emailCC});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
//has to be an ArrayList
ArrayList<Uri> uris = new ArrayList<Uri>();
//convert from paths to Android friendly Parcelable Uri's
for (String file : filePaths)
{
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}
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);
This works for me.
Although this is an old thread, but as it is shown on top on google searches i want to add a small hint to make it complete, hence I stumpled upon it.
It is necessary to make the attached files readable for the mail activity, otherwise they will not be attached. So you have to call somewhere
fileIn.setReadable(true, false)
Here I found great example http://www.blackmoonit.com/2010/02/filebrowser-send-receive-intents/
you must use
final Intent aIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
aIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM,theUris);
aIntent.setType(theOverallMIMEtype);
For multiple attachments use PutParcelableArrayListExtra(Intent.ExtraStream, uris)where uris variable is a List<IParcelable>().
Here's an example:
var email = new Intent(Intent.ActionSendMultiple);
email.SetType("text/plain");
email.PutExtra(Intent.ExtraEmail, new string[]{emailTo});
email.PutExtra(Intent.ExtraCc, new string[]{emailCC});
var uris = new List<IParcelable>();
filePaths.ForEach(file=> {
var fileIn = new File(file);
var uri = Android.Net.Uri.FromFile(fileIn);
uris.Add(uri);
});
email.PutParcelableArrayListExtra(Intent.ExtraStream, uris);
context.StartActivity(Intent.CreateChooser(email, "Send mail..."));
Hope this helps ;)