2011-06-01 121 views
22

注:我不想圖像附加到電子郵件如何在電子郵件正文中顯示圖片?

我想在電子郵件正文中顯示的圖像,

我曾試圖HTML圖像標記<img src=\"http://url/to/the/image.jpg\">"和我輸出,你可以看到我的問題在How to add an image in email body,所以我累Html.ImageGetter

它不工作對我來說,這也給了我同樣的輸出,所以我有一個疑問是有可能做到這一點,

我的代碼

Intent i = new Intent(Intent.ACTION_SEND); 
i.putExtra(Intent.EXTRA_EMAIL,new String[] {"[email protected]"}); 
i.putExtra(Intent.EXTRA_TEXT, 
    Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>", 
    imgGetter, 
    null)); 

i.setType("image/png"); 
startActivity(Intent.createChooser(i,"Email:")); 


private ImageGetter imgGetter = new ImageGetter() { 

    public Drawable getDrawable(String source) { 
     Drawable drawable = null; 
      try { 
       drawable = getResources().getDrawable(R.drawable.icon); 
       drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), 
        drawable.getIntrinsicHeight()); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Log.d("Exception thrown",e.getMessage()); 
      } 
      return drawable; 
    } 
}; 

更新1:如果我用的是ImageGetter代碼TextView我能夠得到的文本和圖像,但我無法看到在電子郵件正文

這裏的形象是我的代碼:

TextView t = null; 
t = (TextView)findViewById(R.id.textviewdemo); 
t.setText(Html.fromHtml("Hi <img src='http://url/to/the/image.jpg'>", 
    imgGetter, 
    null)); 

更新2:我用了大膽的標記,錨標記如圖所示,這些標籤下面我工作正常,但是當我使用img標籤,我可以看到一個方形盒子,其稱作爲OBJ

i.putExtra(Intent.EXTRA_TEXT,Html.fromHtml("<b>Hi</b><a href='http://www.google.com/'>Link</a> <img src='http://url/to/the/image.jpg'>", 
     imgGetter, 
     null)); 
+0

你有沒有試着用另一標籤?當它來自一個意圖時,它可能會被消毒。 – Macarse 2011-06-02 12:22:47

+0

@Macarse:哪個標籤,你可以詳細說明 – 2011-06-02 12:29:01

+0

@Sankar:例如一個大膽的標籤。只是爲了測試它是否有效。 – Macarse 2011-06-02 12:39:15

回答

15

不幸的是,用Intents來做這件事是不可能的。

爲什麼例如大膽文本顯示在EditText上,而不是一個圖像是StyleSplan正在實施ParcelableImageSpan沒有原因。因此,當在新Activity中檢索到Intent.EXTRA_TEXT時,ImageSpan將無法刪除,因此不屬於附加到EditText的樣式的一部分。

由於您無法控制接收活動,因此使用其他不使用Intent傳遞數據的方法是不可行的。

+0

如果這是唯一的區別,你可以非常好地實現你自己的ImageSpan(複製代碼),並通過一個宗地轉移位圖(如果它很小)。艾爾戈,它會unhacel,可以顯示在另一邊? – George 2013-06-10 16:00:43

+11

認真對待什麼?你知道你鏈接的帖子是我的答案的複製/粘貼,而不是其他方式? – rochdev 2013-06-18 10:19:28

+0

有沒有解決這個問題的方法? – HRM 2014-07-01 07:02:54

1

兩個簡單的建議,第一:

  • 閉上你的img標籤(<img src="..." />而不是<img src="...">
  • 使用i.setType("text/html")代替i.setType("image/png")

如果沒有這些工作,也許你試着將圖像附加到電子郵件,然後用"cid:ATTACHED_IMAGE_CONTENT_ID"引用它,而不是一個"http:URL_TO_IMAGE"

Intent i = new Intent(Intent.ACTION_SEND); 
i.putExtra(Intent.EXTRA_EMAIL,new String[] {"[email protected]"}); 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("http://url/to/the/image.jpg"); 
i.putExtra(Intent.EXTRA_TEXT, 
     Html.fromHtml("Hi <img src='cid:image.jpg' />", //completely guessing on 'image.jpg' here 
     imgGetter, 
     null)); 
i.setType("image/png"); 

見標題爲部分發送HTML格式的電子郵件帶有嵌入式圖像apache email user guide

雖然,那麼你就需要知道所連接的圖像的內容ID,和我不知道如果這是通過標準的意圖方法浮出水面。也許你可以檢查原始郵件並確定他們的命名約定?

+0

感謝您的重播,但您的建議不起作用,我曾經使用過,可能在Intent方法中存在一些限制。 – 2011-06-21 09:56:05

1

我知道這並不回答原來的問題,但對某些人來說可能是一種可接受的選擇,而不是將圖像附加到電子郵件。我設法用下面的代碼來實現這個...

String urlOfImageToDownload = "https://ssl.gstatic.com/s2/oz/images/google-logo-plus-0fbe8f0119f4a902429a5991af5db563.png"; 

// Start to build up the email intent 
Intent i = new Intent(Intent.ACTION_SEND); 
i.setType("message/rfc822"); 
i.putExtra(Intent.EXTRA_EMAIL, new String[] { "[email protected]" }); 
i.putExtra(Intent.EXTRA_SUBJECT, "Check Out This Image"); 
i.putExtra(Intent.EXTRA_TEXT, "There should be an image attached"); 

// Do we need to download and attach an icon and is the SD Card available? 
if (urlOfImageToDownload != null && Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 
    // Download the icon... 
    URL iconUrl = new URL(urlOfImageToDownload); 
    HttpURLConnection connection = (HttpURLConnection) iconUrl.openConnection(); 
    connection.setDoInput(true); 
    connection.connect(); 
    InputStream input = connection.getInputStream(); 
    Bitmap immutableBpm = BitmapFactory.decodeStream(input); 

    // Save the downloaded icon to the pictures folder on the SD Card 
    File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    directory.mkdirs(); // Make sure the Pictures directory exists. 
    File destinationFile = new File(directory, attachmentFileName); 
    FileOutputStream out = new FileOutputStream(destinationFile); 
    immutableBpm.compress(Bitmap.CompressFormat.PNG, 90, out); 
    out.flush(); 
    out.close(); 
    Uri mediaStoreImageUri = Uri.fromFile(destinationFile);  

    // Add the attachment to the intent 
    i.putExtra(Intent.EXTRA_STREAM, mediaStoreImageUri); 
}      

// Fire the intent 
startActivity(i); 

http://www.oliverpearmain.com/blog/android-how-to-launch-an-email-intent-attaching-a-resource-via-a-url/

0
i resolve problem send image as a body mail in android 

    you have need three lib of java mail 

    1.activation.jar 
    2.additionnal.jar 
    3.mail.jar 




    public class AutomaticEmailActivity extends Activity { 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      if (android.os.Build.VERSION.SDK_INT > 9) { 
       StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
       StrictMode.setThreadPolicy(policy); 
      } 



      final String fromEmail = "[email protected]"; //requires valid gmail id 
      final String password = "abc"; // correct password for gmail id 
      final String toEmail = "[email protected]"; // can be any email id 

      System.out.println("SSLEmail Start"); 
      Properties props = new Properties(); 
      props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host 
      props.put("mail.smtp.socketFactory.port", "465"); //SSL Port 
      props.put("mail.smtp.socketFactory.class", 
        "javax.net.ssl.SSLSocketFactory"); //SSL Factory Class 
      props.put("mail.smtp.auth", "true"); //Enabling SMTP Authentication 
      props.put("mail.smtp.port", "465"); //SMTP Port 

      Authenticator auth = new Authenticator() { 
       //override the getPasswordAuthentication method 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(fromEmail, password); 
       } 
      }; 

      final Session session = Session.getDefaultInstance(props, auth); 
      Button send_email=(Button) findViewById(R.id.send_email); 
      send_email.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 

        sendImageEmail(session, toEmail,"SSLEmail Testing Subject with Image", "SSLEmail Testing Body with Image"); 

       } 
      }); 

     } 




     public static void sendImageEmail(Session session, String toEmail, String subject, String body){ 
      try{ 
       MimeMessage msg = new MimeMessage(session); 
       msg.addHeader("Content-type", "text/HTML; charset=UTF-8"); 
       msg.addHeader("format", "flowed"); 
       msg.addHeader("Content-Transfer-Encoding", "8bit"); 

       msg.setFrom(new InternetAddress("[email protected]", "NoReply-JD")); 

       msg.setReplyTo(InternetAddress.parse("[email protected]", false)); 

       msg.setSubject(subject, "UTF-8"); 

       msg.setSentDate(new Date()); 

       msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false)); 

       MimeMultipart multipart = new MimeMultipart("related"); 

       BodyPart messageBodyPart = new MimeBodyPart(); 
       String htmlText = "<H1>Hello</H1><img src=\"cid:image\">"; 
       messageBodyPart.setContent(htmlText, "text/html"); 
       // add it 
       multipart.addBodyPart(messageBodyPart); 


       String base = Environment.getExternalStorageDirectory().getAbsolutePath().toString(); 
       String filename = base + "/photo.jpg"; 

       messageBodyPart = new MimeBodyPart(); 
       DataSource fds = new FileDataSource(filename); 

       messageBodyPart.setDataHandler(new DataHandler(fds)); 
       messageBodyPart.setHeader("Content-ID", "<image>"); 
       multipart.addBodyPart(messageBodyPart); 
       msg.setContent(multipart); 


       // Send message 
       Transport.send(msg); 
       System.out.println("EMail Sent Successfully with image!!"); 
      }catch (MessagingException e) { 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
+0

這是從我身邊罰款工作,你可以發送郵件沒有意圖的行動... – 2014-12-23 09:37:03

+0

Bishno這不解決問題,使應用程序用戶發送電子郵件與'html'內容。 – 2018-01-20 20:16:14

相關問題