Display .mht file on android

后端 未结 2 1123
北海茫月
北海茫月 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 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);
            }
        }
    

提交回复
热议问题