"how to get file type from uri kotlin" Code Answer's
You're definitely familiar with the best coding language Kotlin that developers use to develop their projects and they get all their queries like "how to get file type from uri kotlin" answered properly. Developers are finding an appropriate answer about how to get file type from uri kotlin related to the Kotlin coding language. By visiting this online portal developers get answers concerning Kotlin codes question like how to get file type from uri kotlin. Enter your desired code related query in the search bar and get every piece of information about Kotlin code related question on how to get file type from uri kotlin.
how to get file type from uri kotlin
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// my_button = id in activity_main layout
// need for call intent
my_button.setOnclickListener{
val intent = Intent(Intent.ACTION_GET_CONTENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "*/*"
}
startActivityForResult(intent, PICK_ANY_FILE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (resultCode == RESULT_OK && resultCode == PICK_ANY_FILE) {
if (resultData != null) {
resultData.data?.also { uri ->
// usage for function
println( getFileExtension(uri)!! )
}
}
}
}
companion object{
const val PICK_ANY_FILE = 100
/* get actual file name or extension */
fun Context.getFileExtension(uri: Uri): String? = when (uri.scheme) {
// get file extension
ContentResolver.SCHEME_FILE -> File(uri.path!!).extension
// get actual name of file
//ContentResolver.SCHEME_FILE -> File(uri.path!!).name
ContentResolver.SCHEME_CONTENT -> getCursorContent(uri)
else -> null
}
private fun Context.getCursorContent(uri: Uri): String? = try {
contentResolver.query(uri, null, null, null, null)?.let { cursor ->
cursor.run {
val mimeTypeMap: MimeTypeMap = MimeTypeMap.getSingleton()
if (moveToFirst()) mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri))
// case for get actual name of file
//if (moveToFirst()) getString(getColumnIndex(OpenableColumns.DISPLAY_NAME))
else null
}.also { cursor.close() }
}
} catch (e: Exception) {
null
}
}
}
All those coders who are working on the Kotlin based application and are stuck on how to get file type from uri kotlin can get a collection of related answers to their query. Programmers need to enter their query on how to get file type from uri kotlin related to Kotlin code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to get file type from uri kotlin for the programmers working on Kotlin code while coding their module. Coders are also allowed to rectify already present answers of how to get file type from uri kotlin while working on the Kotlin language code. Developers can add up suggestions if they deem fit any other answer relating to "how to get file type from uri kotlin". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to get file type from uri kotlin resolved professionally and stay updated to the latest Kotlin updates.