doc文件需要在文件名附近显示doc徽标,例如whatsapp

时间:2018-08-16 06:53:06

标签: java android chat whatsapp

我试图将文档文件共享给聊天应用程序中的其他人,例如whatsapp。在这里我想在文件名附近显示文件类型徽标。如果是pdf文件pdf徽标,如果是doc文件则需要在文件名附近显示doc logo。我该如何动态地做。那里有任何默认模块

1 个答案:

答案 0 :(得分:0)

我遇到了这个问题,这是我的解决方法:

从文件URI中检测MIME类型

public static String getMimeType(Context context, Uri uri) {
        String mimeType;
        if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
            ContentResolver cr = context.getApplicationContext().getContentResolver();
            mimeType = cr.getType(uri);
        } else {
            String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri
                    .toString());
            mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                    fileExtension.toLowerCase());
        }
        return mimeType;
}

在您的应用中定义支持的MIME类型

public final class MIMEType {
    public static final String IMAGE = "image/*";
    public static final String TEXT_PLAIN = "text/plain";
    public static final String APPLICATION_PDF = "application/pdf";
    public static final String APPLICATION_FLASH = "application/x-shockwave-flash";
    public static final String APPLICATION_OCTET_STREAM = "application/octet-stream";
    public static final String APPLICATION_DOC = "application/msword";
    public static final String APPLICATION_DOCX = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
    public static final String APPLICATION_XLS = "application/vnd.ms-excel";
    public static final String APPLICATION_XLSX = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    public static final String APPLICATION_PPT = "application/vnd.ms-powerpoint";
    public static final String APPLICATION_PPTX = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
}

编写一种从MIME类型获取图标(徽标)资源的方法

private int getIconResourceBasedOnMimeType(String mimeType) {
    switch (mimeType) {
        case MIMEType.TEXT_PLAIN:
            return R.drawable.ic_text_blue;
        case MIMEType.APPLICATION_PDF:
            return R.drawable.ic_pdf_blue;
        case MIMEType.APPLICATION_FLASH:
            return R.drawable.ic_swf_blue;
        case MIMEType.APPLICATION_DOC:
        case MIMEType.APPLICATION_DOCX:
            return R.drawable.ic_word_blue;
        case MIMEType.APPLICATION_XLS:
        case MIMEType.APPLICATION_XLSX:
            return R.drawable.ic_excel_blue;
        case MIMEType.APPLICATION_PPT:
        case MIMEType.APPLICATION_PPTX:
            return R.drawable.ic_pp_blue;
        case MIMEType.APPLICATION_OCTET_STREAM:
        default:
            return R.drawable.ic_binary_blue;
    }
}

希望这对您有所帮助!