java - 在内联电子邮件正文中发送 Base64 编码图像

标签 java android base64 html-email

我正在尝试通过嵌入数据的 html 以电子邮件正文的内联形式发送图像。html 显示正确,但图像现在显示在图像的位置,我看到一个写有“obj”的小块。我还将位图img转换为base64形式。代码如下:

public void imageRetrieved(byte[] img) 
{
        Bitmap newImg=BitmapFactory.decodeByteArray(img,0,img.length);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    newImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.d("LOOK", imageEncoded);

    String txtBody = "<html><body><h1>hi it is stoneage product</h1><br><img src ='data:image/jpeg;base64,"+imageEncoded+"'/></body></html>";
    Log.d("data", txtBody);

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.setType("text/html");           
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "testemail");    
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(txtBody));
    startActivity(Intent.createChooser(emailIntent, "Email:"));

}

请帮帮我

最佳答案

试试这个,它对我有用

    File pngDir = new File(Environment.getExternalStorageDirectory(),"saved_images/"); 
    if (!pngDir.exists())
    {
        pngDir.mkdirs();
    }              
    File pngfile=new File(pngDir,"stoneage.jpg");
    Uri pngUri =Uri.fromFile(pngfile);
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[]{""});
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "hi it is stoneage product Hi this is test mail with attachment");
    emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
    emailIntent.setType("message/rfc822");
    startActivity(Intent.createChooser(emailIntent, "Email:"));

关于java - 在内联电子邮件正文中发送 Base64 编码图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831514/

相关文章:

android - Vuforia - Android 入门

javascript - 将音频文件转换为base64

java - 从用户代理中提取手机名称

java - 包中的类如何使用同一包中其他类的静态方法?

java - 如何通过单击编辑 JTree 节点

java - 在android中从web服务加载图像时如何处理java.io.FileNotFoundException

c# - 如何使用参数打开外部 Java 控制台应用程序、捕获输出并在其上执行命令?

Android WallpaperManager 裁剪图像

android - Html.imageGetter 不显示 Base64 android 中的图像

database - 二进制数据的十六进制编码的目的是什么?