以下代码有问题吗?

时间:2011-05-20 07:43:15

标签: android

我在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用户的文件,点击后会直接将事件添加到mac计算机中的iCal应用程序。

尝试失败

我已经尝试了以下但是它们不起作用:

  1. 将MIME类型更改为text / html

  2. 尝试发送这样的extrastream:-i.putExtra(Intent.EXTRA_STREAM,Uri.parse(“file://”+ attachmentFilePath));?

  3. 提前谢谢。

1 个答案:

答案 0 :(得分:0)

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();
    }
}

此代码正常工作,发送邮件后您可以从SD卡中删除您的文件