我从Gallery获取媒体文件。
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*,video/*,audio/*");
startActivityForResult(Intent.createChooser(intent, "Select Media"), CameraUtils.REQUEST_GALLERY);
如何在onActivityResult()中确定媒体文件的类型?
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CameraUtils.REQUEST_GALLERY:
Uri uri = data.getData();
//what type of media file?
break;
}
}
}
答案 0 :(得分:10)
试试这个:
ContentResolver cr = this.getContentResolver();
String mime = cr.getType(uri);
答案 1 :(得分:0)
仅供参考,在 Kotlin 中我们可以为此创建一个扩展函数。
fun Activity.getMimeType(uri:Uri?):String?{
this.let {
val cr: ContentResolver = it.contentResolver
if (uri == null) return null
return cr.getType(uri)?:null
}
}
随意用空字符串或其他东西替换空值。但这很好用!