1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

画像URIから拡張子を取得する方法について解説。

コード

URIにはcontentスキームfileスキームの2種類が存在するので、それぞれの方法で拡張子を取得する。

fun Uri.getExtension(contentResolver: ContentResolver): String? {
	return when (scheme) {
		ContentResolver.SCHEME_CONTENT -> MimeTypeMap.getSingleton().getExtensionFromMimeType(contentResolver.getType(this))
		ContentResolver.SCHEME_FILE -> MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(File(path ?: return null)).toString())
		else -> null
	}
}

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?