I've seen several exemples but still don't get why, when I'm editing the mail I see the .xml attached but when I receive ther's no attachment!
Here is my code
File f = new File("data/data/xxx/files/xxx.xml");
Boolean b1 = f.exists();
Boolean b2 = f.canRead();
if (b1 && b2) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_EMAIL, "");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" +
f.getAbsolutePath()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "XXX");
sendIntent.putExtra(Intent.EXTRA_TEXT, R.string.mail_body);
startActivity(Intent.createChooser(sendIntent, "Email:"));
} else {
...
Ah, only a detail...when I choose the app to send there is no subject or body, even if I wrote putExtra(Intent.EXTRA_SUBJECT) and putExtra(Intent.EXTRA_TEXT), but that's a detail...
Edit: I just debuged my intent: it says "NOT CACHED" in value of the stream, how to solve it?
You can't attach a file from internal storage directly for some security purpose, hence first you have to copy that file from internal to external directory and then mail after that if you want you can delete that file from external storage in onActivityResult() method.
Here's a code :
private File copyFileToExternal(String fileName) {
File file = null;
String newPath = Environment.getExternalStorageState()+"/folderName/";
try {
File f = new File(newPath);
f.mkdirs();
FileInputStream fin = openFileInput(fileName);
FileOutputStream fos = new FileOutputStream(newPath + fileName);
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = fin.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fin.close();
fos.close();
file = new File(newPath + fileName);
if (file.exists())
return file;
} catch (Exception e) {
}
return null;
}
Method to Email:
private void sendEmail(String email) {
File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
Uri path = Uri.fromFile(file);
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("application/octet-stream");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
String to[] = { email };
intent.putExtra(Intent.EXTRA_EMAIL, to);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putExtra(Intent.EXTRA_STREAM, path);
startActivityForResult(Intent.createChooser(intent, "Send mail..."),
1222);
}
and then
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1222) {
File file = new File(Environment.getExternalStorageState()+"/folderName/" + fileName+ ".xml");
file.delete();
}
}
Call this method like this:
copyFileToExternal(filename + ".xml");
sendEmail(EmailId);
I've seen several exemples but still don't get why, when I'm editing the mail I see the .xml attached but when I receive ther's no attachment!
First, third-party apps cannot read internal storage of your app.
Second, that might not be the right path to internal storage of your app. Never hardcode paths. Your app will fail for secondary accounts and restricted profiles on Android 4.2 tablets, for example. Always use a method, like getFilesDir()
, to get at your portion of internal storage.
You will need to either copy your file to external storage, or better yet, use FileProvider
to serve up your file from internal storage via a content://
Uri
.