Something wrong with the following code?
i have made an application in android 2.2. In one of the activities i am supposed to send an attachment in an email. So for that i have written the following code:
public void emailButtonClicked(View v) {
Intent emailintent = new Intent(Intent.ACTION_SEND);
try {
HandleDatabase hb = new HandleDatabase();
String data = hb.getAttachmentInfo(id);
String details[] = data.split("--");
String formatteddates[] = formatdate(details);
details[2] = formatteddates[0];
details[3] = formatteddates[1];
InputStream myInput = this.getAssets().open("exportformat.txt");
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(myInput));
String outFileName = "/data/data/com.helios.NauticDates/attachment.ics";
File checkfile = new File(outFileName);
if (checkfile.exists()) {
checkfile.delete();
}
for (int i = 0; i < 4; i++) {
if (details[i].equals("null"))
details[i] = " ";
}
FileOutputStream myOutput = new FileOutputStream(outFileName);
String datafromfile;
while ((datafromfile = inputStream.readLine()) != null) {
StringBuffer sb = new StringBuffer(datafromfile);
if (datafromfile.equals("DTSTART:"))
sb.append(details[2]);
if (datafromfile.equals("DTEND:"))
sb.append(details[3]);
if (datafromfile.equals("SUMMARY:"))
sb.append(details[0]);
if 开发者_开发知识库(datafromfile.equals("DESCRIPTION:"))
sb.append(details[1]);
if (datafromfile.equals("CATEGORIES:"))
sb.append(details[4]);
datafromfile = sb.toString();
datafromfile += "\n";
byte[] temp = datafromfile.getBytes();
myOutput.write(temp);
}
// Close the streams
myOutput.flush();
myOutput.close();
inputStream.close();
File tempfile = new File(outFileName);
emailintent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(tempfile));
emailintent.setType("plain/text");
startActivity(Intent.createChooser(emailintent, "Send mail..."));
} catch (Exception e) {
e.printStackTrace();
}
}
In the above , the attachment is visible when the user starts the default email client, but when the user sends the email, the attachment is not received. the body, subject.. everything but the attachment is being received. what is going wrong here.
NOTE
An ics file is a file for mac users which when clicked will directly add an event to the iCal app in the mac computer.
Failed attempts
i have already tried the following but they don't work:
changed the MIME type to text/html
tried sending the extrastream like this:-i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attachmentFilePath));?
thank you in advance.
public void emailButtonClicked(View v) {
Intent emailintent = new Intent(Intent.ACTION_SEND);
try {
String data = "kj--hhfjdfh--hjhwjfh--hdjqhf--bjfhejwfh";
String details[] = data.split("--");
String formatteddates[] = new String[5] ;
formatteddates[0] = "1";
formatteddates[1] = "2";
formatteddates[2] = "3";
formatteddates[3] = "4";
formatteddates[4] = "5";
details[2] = formatteddates[0];
details[3] = formatteddates[1];
InputStream myInput = this.getAssets().open("file.rtf");
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(myInput));
String outFileName ="sdcard"
+ File.separator + "myfile.ics";
File checkfile = new File(outFileName);
if (checkfile.exists()) {
checkfile.delete();
}
for (int i = 0; i < 4; i++) {
if (details[i].equals("null"))
details[i] = " ";
}
FileOutputStream myOutput = new FileOutputStream(outFileName);
String datafromfile;
while ((datafromfile = inputStream.readLine()) != null) {
StringBuffer sb = new StringBuffer(datafromfile);
if (datafromfile.equals("DTSTART:"))
sb.append(details[2]);
if (datafromfile.equals("DTEND:"))
sb.append(details[3]);
if (datafromfile.equals("SUMMARY:"))
sb.append(details[0]);
if (datafromfile.equals("DESCRIPTION:"))
sb.append(details[1]);
if (datafromfile.equals("CATEGORIES:"))
sb.append(details[4]);
datafromfile = sb.toString();
datafromfile += "\n";
byte[] temp = datafromfile.getBytes();
myOutput.write(temp);
}
// Close the streams
myOutput.flush();
myOutput.close();
inputStream.close();
File tempfile = new File(outFileName);
emailintent.setType("text/calendar");
emailintent.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(tempfile));
emailintent.putExtra(Intent.EXTRA_EMAIL, new String[]{"abc@gmail.com"});
System.out.println("URI file=============>"+ "sdcard"
+ File.separator + "myfile.ics");
startActivity(Intent.createChooser(emailintent, "Send mail..."));
} catch (Exception e) {
e.printStackTrace();
}
}
this code is working, after sending mail you may delete your file from sdcard
精彩评论