Я сделал приложение в Android 2.2. В одном из действий я должен отправить приложение в электронном письме. Поэтому для этого я написал следующий код:
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();
}
}
В приведенном выше примере вложение отображается, когда пользователь запускает почтовый клиент по умолчанию, но когда пользователь отправляет электронное письмо, вложение не принимается. тело, субъект.. все, кроме привязанности, получают. что здесь не так.
Примечание
Файл ics - это файл для пользователей Mac, который при нажатии будет напрямую добавлять событие в приложение iCal на компьютере Mac.
Неудачные попытки
Я уже пробовал следующее, но они не работают:
изменил тип MIME на текст /html
попытался отправить extrastream следующим образом: -i.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://" + attachmentFilePath));
благодарим вас заранее.
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[]{"[email protected]"});
System.out.println("URI file=============>"+ "sdcard"
+ File.separator + "myfile.ics");
startActivity(Intent.createChooser(emailintent, "Send mail..."));
} catch (Exception e) {
e.printStackTrace();
}
}
этот код работает, после отправки почты вы можете удалить свой файл из sdcard