电子邮件中的图像预览从资产文件夹加载时不显示意图

时间:2013-07-10 10:17:20

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

我有以下代码:

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

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

任何人都有解决方案吗?

1 个答案:

答案 0 :(得分: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]);
相关问题