1

我正在附加可绘制文件夹中的图像并通过电子邮件发送。当我从默认电子邮件客户端发送它时。附件中缺少图像扩展名(.png),并且文件名本身也已更改。我想发送具有默认名称(如可绘制)和 .png 扩展名的图像。

这是我的代码。

    Intent email = new Intent(Intent.ACTION_SEND);
            email.setType("image/png");

            email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });

            email.putExtra(Intent.EXTRA_SUBJECT, "Hey! ");


            email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));
startActivity(Intent.createChooser(email, "Sending........"));

请建议我这段代码中的内容,谢谢。

4

1 回答 1

1

如果图像位于 SDCARD 中,您只能将图像附加到邮件中。因此,您需要将图像复制到 SD,然后附加。

InputStream in = null;
OutputStream out = null;
try
{
in = getResources().openRawResource(R.raw.ic_launcher);
out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png"));
copyFile(in, out);
 in.close();
 in = null;
 out.flush();
out.close();
out = null;
}
catch (Exception e)
                {
                    Log.e("tag", e.getMessage());
                    e.printStackTrace();
                }

private void copyFile(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }


Intent emailIntent = new Intent(Intent.ACTION_SEND);
                emailIntent.setType("text/html");
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
                Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png"));
                emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(emailIntent, "Send mail..."));
于 2012-10-19T13:48:18.990 回答