我正在附加可绘制文件夹中的图像并通过电子邮件发送。 当我从默认电子邮件客户端发送它时。附件中缺少图像扩展名(.png) 并且文件名也会自行更改。 我希望使用默认名称(如drawable)和.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........"));
请告诉我这段代码中有什么内容 感谢。
答案 0 :(得分:1)
如果图像位于SD卡中,则只能将图像附加到邮件中。 因此,您需要将图像复制到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..."));