Android - 资源类型 MINE Type
一、概念
MINE(Multipurpose Internet Mail Extensions)最初是为了标识电子邮件附件的类型,在 HTML 中使用 content-type 属性表示,描述了文件类型的互联网标准。
格式:媒体类型/子类型,可使用通配符*。如 audio/mp3,image/*。
二、使用场景
2.1 Intent
对于隐式意图,在 xml 中对 Activity 的 Intent Filter 指定,表示可以打开的资源类型,文件管理器中长按图片选择打开方式。
<activityandroid:name=".base.MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /><data android:mimeType="image/*"/></intent-filter>
</activity>
2.2 ContentProvider
在自定义的内容提供者中,会重写 getType() 来返回 URI 的 MINE Type。
class MyContentProvider : ContentProvider() {override fun getType(uri: Uri): String? {val code = uriMacher.match(uri)//返回该 URI 对应数据的 MIME 类型return when(code) {MATCH_CODE_A -> “audio/mp3”MATCH_CODE_B -> "image/*"else -> null //都不匹配返回null}}
}