使用图片附件发送电子邮件

时间:2017-01-11 08:34:26

标签: android share email-attachments

我需要使用Android共享意图发送一封电子邮件,其中包含需要使用网络请求获取的图片。

我发现的大部分示例涉及已保存到设备的图像。

如果需要首先获取图像而不必将其保存到磁盘,我该如何实现?

我使用Glide作为我的图像加载器。

1 个答案:

答案 0 :(得分:1)

您可以使用滑行将图像保存到缓存目录中,并使用以下代码将其作为附件发送

   Glide
.with(getApplicationContext())
.load("https://www.google.es/images/srpr/logo11w.png") // your URL
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
    @Override
    public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
     File f = new File(context.getCacheDir(), filename);// use your filename fully
    f.createNewFile();
    //Convert bitmap to byte array
    Bitmap bitmap = resource;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    Uri U = Uri.fromFile(f);
    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("image/png");
    i.putExtra(Intent.EXTRA_STREAM, U);
    startActivity(Intent.createChooser(i,"Email:"));  
     }
});