【问题标题】:Image preview in email Intent not showing when loaded from Assets Folder从资产文件夹加载时,电子邮件中的图像预览意图不显示
【发布时间】:2013-07-10 10:17:20
【问题描述】:

我有类似下面的代码:

public void shareImageInEmail(String imageUri){
   Intent emailIntent = new Intent(Intent.ACTION_SEND);
   emailIntent.setType("message/rfc822");
   emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   emailIntent.putExtra(Intent.EXTRA_TEXT, "Some text");
   emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageUri));
   mActivity.startActivity(emailIntent);
}

Uri 从媒体文件夹(相机相册等)中获取时,一切正常。 问题是当我像这样从资产文件夹中获取Uri 时:

share("content://com.ex.myapp/logo.png");

在这种情况下,共享工作正常,但是当打开电子邮件客户端时,图像预览是一个灰色框,而不是实际图像。当我发送图片时发送正确,它只是不显示预览。

有人有解决办法吗?

【问题讨论】:

  • 这样设置类型 / 而不是 message/rfc822
  • 斜线前后必须是星号(*)。这个评论框不支持或者我不知道怎么写
  • 嗨,迪恩。感谢您的回答,但这并没有解决问题。

标签: android android-intent share assets email-attachments


【解决方案1】:

一个简单的解决方案是将 Assets 中的所有内容复制到 Sdcard 并将“Sdcard path Uri”作为 EXTRA_STREAM 传递给电子邮件。

示例代码:

public void shareImageInEmail(String imageUri){
       Intent emailIntent = new Intent(Intent.ACTION_SEND);        
       emailIntent.setType("message/rfc822");

       emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       emailIntent.putExtra(Intent.EXTRA_TEXT, "Some text");

       Log.v(TAG, "imageUri, file://" + imageUri);
       emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + imageUri));
       startActivity(emailIntent);
}

将所有资产复制到 SDCard(参考:How to copy files from 'assets' folder to sdcard?

new File(Environment.getExternalStorageDirectory(), filename); //Store in Sdcard

最后调用shareImageInEmail如下,

shareImageInEmail(Environment.getExternalStorageDirectory() + "/Image.png");//assets[0]);

【讨论】:

  • 感谢您的回复,我将其标记为已解决,因为我使用了您的方法。基本上是因为其他包无法直接访问我的包资产。我正在使用 AssetManager.open(uri.getPath().substring(1)) 创建一个文件,然后将此文件作为 EXTRA_STREAM 添加到意图中。这解决了我的应用程序的问题。
猜你喜欢
  • 1970-01-01
  • 2011-11-06
  • 1970-01-01
  • 2019-09-13
  • 1970-01-01
  • 1970-01-01
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多