Display .mht file on android

后端 未结 2 1068
北海茫月
北海茫月 2020-12-20 23:06

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

相关标签:
2条回答
  • 2020-12-20 23:24

    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);
            }
        }
    
    0 讨论(0)
  • 2020-12-20 23:32

    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.

    0 讨论(0)
提交回复
热议问题