在Android中将来自URL的图像作为附件电子邮件发送

4

我有一个问题,我想发送带有图片附件的电子邮件,但该图片在url上。我无法发送它。请为正确结果提供建议。

提前致谢。

以下是代码:

 btn_mail.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                setImage(item.getImageUrl());
                if(item instanceof Product)
                {
                body = "<html><body>Found this a great deal on <a href=http://www.bizrate.com>@Bizrate</a><a href="+item.getUrl()+"> "+item.getTitle()+"</a><br><br><img src="+item.getImageUrl(100)+"></body></html>";
                }else
                {
                    Offer offer = (Offer)item;
                    body = "<html><body>Found this a great deal on <a href=http://www.bizrate.com>@Bizrate</a><a href="+item.getUrl()+"> "+item.getTitle()+"</a><br><br><img src="+item.getImageUrl(100)+"></body></html>";
                }
                Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, item.getTitle());
                /*emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                        "Title: " + item.getTitle() + "\n" +
                                "Description: " + item.getDescription() + "\n" + "\n" +
                                "Max Price: " + max_price + "\n" +
                                "Min Price: " + min_price);*/
                emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,Html.fromHtml(body));
                //emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://com.shopzilla.android.common/" + R.drawable.barcode));
                //emailIntent.putExtra(Intent.EXTRA_STREAM, imageBitmap);
                emailIntent.setType("message/rfc822");
                context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
            }
        });


  private void setImage(String string) {
        try {
            URL url = new URL(string);
            imageBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());

        } catch (MalformedURLException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

嘿,我遇到了同样的问题,如果你解决了这个问题,能否请你发布一下代码? - ilovebali
2个回答

6
<img> 标签无法使用。要将图像作为附件发送,您必须将其保存到SD卡中。
您需要添加:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

将以下内容添加到您的AndroidManifest.xml文件中。

要保存图像,请执行以下操作(在后台线程中完成):

try {
    File rootSdDirectory = Environment.getExternalStorageDirectory();

    File pictureFile = new File(rootSdDirectory, "attachment.jpg");
    if (pictureFile.exists()) {
        pictureFile.delete();
    }                   
    pictureFile.createNewFile();

    FileOutputStream fos = new FileOutputStream(pictureFile);

    URL url = new URL("http://your_image_server/dummy.jpg");
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setDoOutput(true);
    connection.connect();
    InputStream in = connection.getInputStream();

    byte[] buffer = new byte[1024];
    int size = 0;
    while ((size = in.read(buffer)) > 0) {
        fos.write(buffer, 0, size);
    }
    fos.close();

} catch (Exception e) {
    e.printStackTrace();
    return null;
}

保存图像后,获取其Uri并将其发送到意图中(在主线程中):
Uri pictureUri = Uri.fromFile(pictureFile);
emailIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);

希望这能帮到您:)

0

你可以用这段代码很快地完成那个任务

protected Uri getImageUri(String imgTitle,Bitmap inImage) {
if(inImage == null) {
return null;
}

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getContentResolver(), inImage, imgTitle, null);
return Uri.parse(path);
}

位图来自ImageView,您可以轻松从中获取

((BitmapDrawable)img.getDrawable()).getBitmap()

为了不阻塞主应用程序的行为,必须在线程(或处理程序)中执行getImageUri。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接