【问题标题】:send base64 encoded image in inline email body在内联电子邮件正文中发送 base64 编码图像
【发布时间】:2013-11-18 20:05:29
【问题描述】:

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

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:"));

}

请帮帮我

【问题讨论】:

    标签: java android base64 html-email


    【解决方案1】:

    试试这个对我有用

        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:"));
    

    【讨论】:

    • 如果不起作用,则将位图图像保存到 sdcard 并为其提供该路径
    • 我也这样做了,但我不知道为什么仍然显示“obj”代替图像
    • 现在我的代码就像

      嗨,它是石器时代的产品


    • 感谢您的回复@Amit,但新的更新代码也无法正常工作,同样的“obj”显示在图片的位置
    • 请您尝试将已保存在 sdcard 上的图像附加到未转换为 base64 的图像
    猜你喜欢
    • 2013-04-20
    • 2011-07-26
    • 2011-06-08
    • 1970-01-01
    • 2014-02-16
    • 2011-12-09
    • 2015-09-21
    • 2018-07-04
    • 2017-07-08
    相关资源
    最近更新 更多