简体   繁体   中英

Best view to display images and html content in android

*I am working on an application which can read emails. I am using textview / edittext to display the mails. Right now I am able to fetch the email content as string and display it. But Which is the best view to display emails with html content and images?? Please help me out. Thank you :) Edited: I used webview as suggested by SO friends(thanks to @Andro Selva, @CFlex). But I am facing a problem, Its displaying the email's body twice! :( Once as a text/plain and again as a Text/html. 当发送空白邮件时,它是一个带有电子邮件消息的webview的screeshot I have tried following codes to load webview.

webViewBody.loadData(details[3], "text/html", "UTF-8");
//webViewBody.loadDataWithBaseURL(null, details[3] , "text/html", "utf-8", null);

here details[3] is the email content. What am i missing here!? :( Please help me out.

EDITED: I have Added codes I have Used to get the message contents for displaying email content below.

    public void getContent(Message msg)
{
     try
     {
        Object o = msg.getContent();
            if (o instanceof String) 
        {
        if(((String) o).equalsIgnoreCase(""))
        {
        }
                    else
        {
           messageBody = (String)o+"STRING!!";
        }
       }
       else if (o instanceof Multipart) 
       {
        Multipart mp = (Multipart)o;
            int count3 = mp.getCount();
        for (int j = 0; j < count3-1; j++)
        {
            // Part are numbered starting at 0
            BodyPart b = mp.getBodyPart(j);
            Object o2 = b.getContent();
            if (o2 instanceof String) 
            {
                if(((String) o).equalsIgnoreCase(""))
                {
                }
                                    else
                {
                    messageBody = (String)o2+"MULTIPART!!"; 
                }
            }
        } //End of for
       }
       else if (o instanceof InputStream) 
       {
        //System.out.println("**This is an InputStream message**");
        InputStream is = (InputStream)o;
        // Assumes character content (not binary images)
                    //messageBody = convertStreamToString(is)+"INPUT STREAM!!";
            int c;
                    while ((c = is.read()) != -1) 
                    {
                    messageBody = convertToString(is)+"INPUT STREAM!!";
                System.out.println(messageBody);
                    }
       }
    }
        catch (Exception ex)
    {
    System.out.println("Exception arise at get Content");
    ex.printStackTrace();
    }

    //TODO TEST CODES
    try
    {
        String contentType = msg.getContentType();
        //          System.out.println("Content Type : " + contentType);
        Multipart mp = (Multipart) msg.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++)
        {
            dumpPart(mp.getBodyPart(i));
        }
    }
    catch (Exception ex)
    {
        System.out.println("Exception arise at get Content");
        ex.printStackTrace();
    }
}

public String convertToString(InputStream inputStream)
{
    StringBuffer string = new StringBuffer();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    String line;
    try 
    {
        while ((line = reader.readLine()) != null) 
        {
            string.append(line + "\n");
        }
    }
    catch (IOException e) 
    {
    }
    return string.toString();
}   

The control is always going to "else if (o instanceof InputStream)" condition. so its streaming email always and displaying complete email content. What am I missing here?

I think you will need to implement a WebView , because it can format the html AND display images.

AFAIK, A textView with html can format the content but cannot display the images.

Edit : The following should work:

webViewBody.loadData(details[3], "text/html", null);

Probably you are displaying all the email content that can be composed by html and plain text content in the same email. This is caused because this kind of email is multipart content and u need to split-it, you also need to separate attachments and images (embedded) see if your "details" is not returning a multipart content body.

The detail gets displayed twice just because of two times you have written:

webViewBody.loadData(details[3], "text/html", "UTF-8");
webViewBody.loadDataWithBaseURL(null, details[3] , "text/html", "utf-8", null);

Probable Solution: Use either loadData() or loadDataWithBaseURL() .

adding these lines solved the problem!

    MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
    mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
    mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
    mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
    mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
    mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
    CommandMap.setDefaultCommandMap(mc);

Thanks to everyone who helped me to solve this. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM