通过Android应用程序将图像附加到电子邮件

时间:2012-08-10 10:20:29

标签: android email

我的应用成功发送电子邮件,但当我尝试附加图片时,代码崩溃了 这是代码片段

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"email address"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
    //  i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);



        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(EidCards.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }

评论的行使代码崩溃 请建议一个出路!

2 个答案:

答案 0 :(得分:1)

这对你不起作用:

i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);

R.drawable.pic4的类型是什么?这不是用于解析drawable的整数吗?

了解其他人如何附加图片: Sharing an image with Google+ app using Intent.ACTION_SEND and Intent.EXTRA_STREAM

答案 1 :(得分:1)

很简单:P

Intent i = new Intent(Intent.ACTION_SEND);

        int imageURI=R.drawable.pic4;

        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"email address"});
        i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
        i.putExtra(Intent.EXTRA_TEXT   , "body of email");
        i.putExtra(Intent.EXTRA_STREAM, R.drawable.pic4);

            i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+getPackageName()+"/"+imageURI));


        try {
            startActivity(Intent.createChooser(i, "Send mail..."));
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(EidCards.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
        }