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

时间:2013-11-07 08:55:30

标签: java android base64 html-email

我正在尝试通过嵌入数据的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:"));

}

亲切地帮助我

1 个答案:

答案 0 :(得分:0)

试试这个对我有用

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