蹊跷的下列code? [英] Something wrong with the following code?

查看:151
本文介绍了蹊跷的下列code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Android 2.2的应用程序。在活动中的一个,我应该在电子邮件中发送附件。在这样的情况我写了下面的code:

 公共无效emailButtonClicked(视图v){
    意图emailintent =新的意图(Intent.ACTION_SEND);
    尝试 {
        HandleDatabase HB =新HandleDatabase();
        字符串数据= hb.getAttachmentInfo(ID);
        字符串的详细信息[] = data.split( - );
        字符串formatteddates [] = formatdate(详细信息);
        细节[2] = formatteddates [0];
        细节[3] = formatteddates [1];
        InputStream的myInput = this.getAssets()开(exportformat.txt)。
        BufferedReader中的InputStream =新的BufferedReader(
                新的InputStreamReader(myInput));

        字符串outFileName =/data/data/com.helios.NauticDates/attachment.ics;
        文件checkfile =新的文件(outFileName);
        如果(checkfile.exists()){
            checkfile.delete();
        }
        的for(int i = 0;我4;;我++){
            如果(详细信息[我] .equals(空))
                详细信息[我] =;
        }
        FileOutputStream中myOutput =新的FileOutputStream(outFileName);
        字符串datafromfile;
        而((datafromfile = inputStream.readLine())!= NULL){
            StringBuffer的SB =新的StringBuffer(datafromfile);
            如果(datafromfile.equals(DTSTART:))
                sb.append(详细[2]);
            如果(datafromfile.equals(DTEND:))
                sb.append(详细信息[3]);
            如果(datafromfile.equals(摘要:))
                sb.append(详细信息[0]);
            如果(datafromfile.equals(描述:))
                sb.append(详细资料[1]);
            如果(datafromfile.equals(类别))
                sb.append(详细[4]);
            datafromfile = sb.toString();
            datafromfile + =\ N的;
            byte []的温度= datafromfile.getBytes();
            myOutput.write(临时);
        }

        //关闭流
        myOutput.flush();
        myOutput.close();
        inputStream.close();
        文件临时文件=新的文件(outFileName);
        emailintent.putExtra(Intent.EXTRA_STREAM,
                Uri.fromFile(临​​时文件));
        emailintent.setType(纯/文);
        startActivity(Intent.createChooser(emailintent,发送邮件...));
    }赶上(例外五){
        e.printStackTrace();
    }
}
 

在上文中,附件是可见当用户开始默认电子邮件客户端,但是,当用户发送电子邮件,未接收到的附件。正在接收主体,主体..一切但附件。 到底是怎么回事错在这里。

注意

ICS文件是,点击后直接添加事件到了Mac电脑的应用程序的iCal Mac用户的文件。

失败的尝试

我已经试过以下,但他们不工作:

  1. 改变MIME类型为text / html

  2. 试图发送这样的extrastream:-i.putExtra(Intent.EXTRA_STREAM,Uri.parse(文件://+ attachmentFilePath));

感谢你在前进。

解决方案

 公共无效emailButtonClicked(视图v){
    意图emailintent =新的意图(Intent.ACTION_SEND);
    尝试 {

        字符串数据=KJ  -  hhfjdfh  -  hjhwjfh  -  hdjqhf  -  bjfhejwfh;
        字符串的详细信息[] = data.split( - );
        串formatteddates [] =新字符串[5];
        formatteddates [0] =1;
        formatteddates [1] =2;
        formatteddates [2] =3;
        formatteddates [3] =4;
        formatteddates [4] =10;
        细节[2] = formatteddates [0];
        细节[3] = formatteddates [1];
        InputStream的myInput = this.getAssets()开(file.rtf)。
        BufferedReader中的InputStream =新的BufferedReader(
                新的InputStreamReader(myInput));

        字符串outFileName =SD卡
        +文件分割符+myfile.ics;
        文件checkfile =新的文件(outFileName);
        如果(checkfile.exists()){
            checkfile.delete();
        }
        的for(int i = 0;我4;;我++){
            如果(详细信息[我] .equals(空))
                详细信息[我] =;
        }
        FileOutputStream中myOutput =新的FileOutputStream(outFileName);
        字符串datafromfile;
        而((datafromfile = inputStream.readLine())!= NULL){
            StringBuffer的SB =新的StringBuffer(datafromfile);
            如果(datafromfile.equals(DTSTART:))
                sb.append(详细[2]);
            如果(datafromfile.equals(DTEND:))
                sb.append(详细信息[3]);
            如果(datafromfile.equals(摘要:))
                sb.append(详细信息[0]);
            如果(datafromfile.equals(描述:))
                sb.append(详细资料[1]);
            如果(datafromfile.equals(类别))
                sb.append(详细[4]);
            datafromfile = sb.toString();
            datafromfile + =\ N的;
            byte []的温度= datafromfile.getBytes();
            myOutput.write(临时);
        }

        //关闭流
        myOutput.flush();
        myOutput.close();
        inputStream.close();
        文件临时文件=新的文件(outFileName);
        emailintent.setType(文本/日历);
        emailintent.putExtra(Intent.EXTRA_STREAM,
                Uri.fromFile(临​​时文件));
        emailintent.putExtra(Intent.EXTRA_EMAIL,新的String [] {abc@gmail.com});

        的System.out.println(URI文件=============>+SD卡
                文件分割符+ +myfile.ics);

        startActivity(Intent.createChooser(emailintent,发送邮件...));
    }赶上(例外五){
        e.printStackTrace();
    }
}
 

这code是工作,发送邮件后,您可以从SD卡中删除文件

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:

  1. changed the MIME type to text/html

  2. 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

这篇关于蹊跷的下列code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆