im trying to make my android application send an email with an xml file as attachement. All is working fine except for the fact that the xml file I receive is empty. I checked to make sure the file isn't empty on my phone...
Here is the code I use to send the mail:
Intent mailIntent = new Intent(Intent.ACTION_SEND);
mailIntent.setType("text/Message");
mailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"someone@somewhere.com"});
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "MySubject");
mailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file///sdcard/rapport.xml"));
startActivity(Intent.createChooser(mailIntent, "Send it out!"));
Thnx in开发者_开发知识库 advance!
I think it might be your file protocol declaration there. You could either try Uri.fromFile, or maybe just use "file:///" (yours appears to be missing the colon, unless that's just a typo here).
http://developer.android.com/reference/android/net/Uri.html#fromFile(java.io.File)
Also, mine is close to yours, but this is how I have done it in the past (and it seems to work):
File f = new File("path_to_file");
if (f.exists() && f.canRead()) {
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setType("text/plain");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" +
f.getAbsolutePath()));
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "SUBJECT");
sendIntent.putExtra(Intent.EXTRA_TEXT, "BODY");
startActivity(Intent.createChooser(sendIntent, "Email:"));
} else {
Toast.makeText(Main.this, getString(R.string.fileNotExistBlah),
Toast.LENGTH_LONG).show();
}
精彩评论