Android - 如何从资源文件夹中嵌入电子邮件正文中的图像

时间:2014-06-18 05:25:39

标签: android image email html-email android-assets

所以,我有这个toHtml函数:

  public String toHtml() {
    Uri imageUri = Uri.fromFile(new File("android_asset/" + getFullAssetsFilepath()));
    String html = getString(R.string.email_image, imageUri);
    html += getString(R.string.email_caption, caption);
    return html;
  }

  <string name="email_image">&lt;p>&lt;img src=\"%1$s\" /></string>
  <string name="email_caption">&lt;br/>%1$s&lt;/p></string>

这是生成的内容:

<p><img src="file:///android_asset/images/00001.jpg" /><br/>Bird</p>

还有其他函数的其他htmls,它们一起传递给这个sendEmail函数:

  private void sendEmail(String subject, String content) {
    Intent email = new Intent(Intent.ACTION_SEND);
    email.putExtra(Intent.EXTRA_TEXT, 
        Html.fromHtml(content + mContext.getString(R.string.email_generated_by_vancouver_trees)));
    email.putExtra(Intent.EXTRA_SUBJECT, subject);
    email.setType("text/html");
    mContext.startActivity(Intent.createChooser(email, getString(R.string.share)));
  }

电子邮件代理知道它是一张图片,但我在电子邮件中收到的是: [OBJ] 鸟

还有另一种方法来为资产生成src吗?我在看Embedding image in email in Android

我也试过这个

XML

  <string name="email_image">&lt;p>&lt;img src=\"data:image/png;base64,%1$s\" /></string>
  <string name="email_caption">&lt;br/>%1$s&lt;/p></string>

java

  public String toHtml(Context context) {
    try {
      InputStream istr = context.getAssets().open(getFullAssetsFilepath());
      Bitmap bitmap = BitmapFactory.decodeStream(istr);
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
      byte[] byteArray = byteArrayOutputStream .toByteArray();
      String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
      String html = context.getString(R.string.email_image, encoded);
      html += context.getString(R.string.email_caption, caption);
      return html;
    } catch (IOException e) {
    }
    return "";
  }

但我仍然以[obj]框

结束

事实上,我已经尝试过src =&#34; [静态图像html]&#34;我仍然得到[obj]

0 个答案:

没有答案