The attachment name in the email contains Chinese characters and displays garbled characters

The attachment name in the Email contains Chinese characters and displays garbled characters because the attachment contains Chinese characters, but the corresponding encoding is not specified, which causes Android to display Chinese characters according to the default encoding UTF-8 and displays garbled characters. The attachment name in the

email contains Chinese characters And specified encode
Content-Type: audio/mpeg; name="=?gb2312?B?us+zyS5tcDM=?="
Content-Description: =?gb2312?B?us+zyS5tcDM=?=
Content-Disposition: attachment; filename="=?gb2312?B?us+zyS5tcDM=?=";
size=6313900; creation-date="Wed, 13 Sep 2017 10:01:46 GMT";
modification-date="Tue, 28 Nov 2017 08 :41:13 GMT"
Content-Transfer-Encoding:


The attachment name in the base64 email contains Chinese characters without specifying encode
----_com.android.email_1076710617800
Content-Type: image/png;
name="zhu.png"
Content- Transfer-Encoding: base64
Content-Disposition: attachment;
filename="zhu.png";
size=58864

修正
android\packages\apps\Email\provider_src\com\android\email\LegacyConversions.java
@VisibleForTesting
    protected static Attachment mimePartToAttachment(final Part part) throws MessagingException {
        // Transfer fields from mime format to provider format
        String contentType = MimeUtility.unfoldAndDecode(part.getContentType());
        String ramContentType = part.getContentType();
        if (ramContentType.indexOf("=?") == -1) {
            LogUtils.v(LogUtils.TAG, "original contentType donnot contain charset and encode");
            String unfoldContentType = MimeUtility.unfold(ramContentType);
            try {
                String charset = getEncoding(unfoldContentType);
                LogUtils.v(LogUtils.TAG, "translate the contentType with: " + charset);
                contentType = new String(unfoldContentType.getBytes(charset), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                LogUtils.v(LogUtils.TAG, "translate the contentType happen UnsupportedEncodingException: ");
                contentType = MimeUtility.unfoldAndDecode(part.getContentType());
            }
        } else {
            contentType = MimeUtility.unfoldAndDecode(part.getContentType());
        }
......


  private static String getEncoding(String str) {
        String encode[] = new String[]{
                "ISO-8859-1",
                "GB2312",
                "GBK",
                "GB18030",
                "Big5",
                "Unicode",
                "ASCII",
                "UTF-8"
        };
        for (int i = 0; i < encode.length; i++) {
            try {
                if (str.equals(new String(str.getBytes(encode[i]), encode[i]))) {
                    return encode[i];
                }
            } catch (Exception ex) {
                LogUtils.v(LogUtils.TAG, "getEncoding: " + ex.toString());
            }
        }

        return "UTF-8";
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326070334&siteId=291194637