在 Android 開發檔案管理器或需喚起其他 App 開啟檔案時,正確的 MIME 類型判斷至關重要。本文提供完整的「副檔名 → MIME」對照表,並封裝為可直接落地的工具類。
Intent.ACTION_VIEW + setDataAndType(Uri, mime) 尋找支援該 MIME 的 Activity,若有多個則彈出選擇器。public class FileUtil {
/** 根據副檔名取得 MIME 類型 */
public static String getMIMEType(File file) {
String type = "*/*"; // 預設未知二進位
String name = file.getName().toLowerCase(Locale.ROOT);
int dot = name.lastIndexOf('.');
if (dot < 0) return type;
String ext = name.substring(dot); // 含小數點,如 ".pdf"
for (String[] pair : MIME_MAP) {
if (ext.equals(pair[0])) return pair[1];
}
return type;
}
/** 喚起系統選擇器開啟檔案 */
public static void openFile(Context context, File file) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
String mime = getMIMEType(file);
// Android 7.0+ 需使用 FileProvider 取得 content:// Uri
Uri uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.setDataAndType(uri, mime);
context.startActivity(intent);
}
// -------- 靜態對照表 (節錄常用,完整見下方) --------
private static final String[][] MIME_MAP = {
{".3gp", "video/3gpp"}, {".apk", "application/vnd.android.package-archive"},
{".pdf", "application/pdf"}, {".png", "image/png"}, {".jpg", "image/jpeg"},
{".mp4", "video/mp4"}, {".mp3", "audio/mpeg"}, {".zip", "application/zip"},
{".txt", "text/plain"}, {".html", "text/html"}, {".doc", "application/msword"},
{".xls", "application/vnd.ms-excel"}, {".ppt", "application/vnd.ms-powerpoint"},
// ... 完整表見下方
};
}
| 副檔名 | MIME 類型 |
|---|---|
| .3gp | video/3gpp |
| .apk | application/vnd.android.package-archive |
| .asf | video/x-ms-asf |
| .avi | video/x-msvideo |
| .bin | application/octet-stream |
| .bmp | image/bmp |
| .c / .cpp / .h | text/plain |
| .class | application/octet-stream |
| .doc | application/msword |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .exe | application/octet-stream |
| .gif | image/gif |
| .gz | application/gzip |
| .htm / .html | text/html |
| .jar | application/java-archive |
| .java | text/plain |
| .jpeg / .jpg | image/jpeg |
| .js | application/javascript |
| .json | application/json |
| .mp3 | audio/mpeg |
| .mp4 | video/mp4 |
| application/pdf | |
| .png | image/png |
| .ppt / .pptx | application/vnd.ms-powerpoint / application/vnd.openxmlformats-officedocument.presentationml.presentation |
| .rar | application/x-rar-compressed |
| .txt | text/plain |
| .xls / .xlsx | application/vnd.ms-excel / application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .xml | text/xml (或 application/xml) |
| .zip | application/zip |
| (未知) | */* |
維護提示:表格會隨 Android 版本與新檔案格式更新,建議專案中維護一份 mime.properties 資源檔,運行時載入,便於熱更新。
嚴格模式禁止 file:// Uri 跨應用傳遞,必須使用 FileProvider 產生 content:// Uri:
<!-- AndroidManifest.xml -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!-- res/xml/file_paths.xml -->
<paths>
<files-path name="internal_files" path="." />
<external-path name="external_files" path="." />
</paths>