Android的附加图像的电子邮件不工作 [英] Android attach image to email doesn't work

查看:145
本文介绍了Android的附加图像的电子邮件不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的应用程序发送电子邮件,它的标志。
但我收到的时候连接字符串格式(应该是PNG)的电子邮件。
我的code:

I'm trying to send email from my application with it's logo.
But I receive the email when the attachment in string format(should be png).
My code:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("application/image");

    intent.putExtra(Intent.EXTRA_SUBJECT, subject);
    intent.putExtra(Intent.EXTRA_TEXT, getString(R.string.fb_share_description));
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://my.package/" + R.drawable.ic_launcher));
   Intent chooser = Intent.createChooser(intent, "share");
   startActivity(chooser);

我应该怎么办?

What should I do?

推荐答案

您不能附加文件从内部资源的电子邮件。你必须将它复制到存储像SD卡头的可公共访问区域。

You cannot attach files to an email from your internal resources. You must copy it to a commonly accessible area of the storage like the SD Card first.

InputStream in = null;
OutputStream out = null;
try {
    in = getResources().openRawResource(R.drawable.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);
    }
}

//Send the file
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..."));

这是必需的,因为只有在读取和​​沙箱到您的应用程序捆绑您的应用程序的资源。该电子邮件客户端接收的URI是它不能访问。

This is required as the resources you bundle with your app are read only and sandboxed to your application. The URI that the email client receives is one it cannot access.

这篇关于Android的附加图像的电子邮件不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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