5

How to display .mht(MHTML) file on android webview. I tried to open the .mht file on default android browser but that didn't open but i am able to open same on opera mobile browser. So i tried with MHTUnpack java library. I didn't succeed in that.

Here's a link!

Please if anybody has used this MHTUnpack let me how can i use that in android. And also let me know if there is any other library.

Thanks

bheema
  • 313
  • 1
  • 5
  • 9

2 Answers2

1

Found this unknown google project which appears to be working.

This utility decodes the mhtml file and save it on given path(internal or external). After saving it returns the html file path which could be loaded into webview.

Try it.

Chitranshu Asthana
  • 1,061
  • 8
  • 15
  • That would require sending the MHTML file back to the server using an AJAX request, then waiting for the result which is not ideal. – Paul Sweatte Aug 02 '12 at 01:24
  • I am sorry but I don't think I understood what you are trying to say here. The Project referred here does all the operation locally. Although the quality of this project is questionable but since it is open source, you can always go ahead and enhance it. – Chitranshu Asthana Aug 02 '12 at 06:22
  • The project is a native Android application, so the user would have to install the project, open the browser, download the MHTML file, run the utility on the MHTML file, reopen the browser, then load the resulting HTML page. – Paul Sweatte Aug 02 '12 at 17:05
  • This answer is outdated. – IgorGanapolsky Nov 03 '17 at 14:11
0

After overcoming frustration about WebView.saveWebArchive() format change in Android 4.4, I tried the "unknown google project" Chitranshu Asthana mentioned in his answer, but code provided there is slow (~10s for 1MB *.mht file with a dozen of pictures) and doesn't handle attached file names correctly.

MHT Unpack library combined with Java Mail for Android (not the one provided by Oracle) worked perfectly for me.


EDIT: Fixed the link to MHT Unpack library. Also, here's usage example:

// contentPath - path to input .mht file
public static String unpackMht(String contentPath) throws IOException {
        // dstPath - path where file will be unpacked
        String dstPath = openTempDir(null) + File.separator;
        String indexFileName = dstPath + new File(contentPath).getName();

        try {
            Collection<Attachment> attachments = MHTUnpack.unpack(new File(contentPath));

            for (Attachment attachment : attachments) {
                String filename = attachment.getFileName();
                String path = filename == null ? indexFileName : dstPath +  filename;
                File newFile = new File(path);
                if (newFile.exists()) {
                    newFile.delete();
                }
                attachment.saveFile(path);
            }

            return indexFileName;
        } catch (MessagingException e) {
            throw new IOException(e);
        }
    }
Andrii Chernenko
  • 8,920
  • 6
  • 65
  • 83