使用Kotlin在Android应用程序中打开文件选择 [英] Open File choose in Android app using Kotlin
本文介绍了使用Kotlin在Android应用程序中打开文件选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
任务:上传用户可以从设备中选择的图像.
如何使用Kotlin在Android应用中按下按钮时打开文件选择器"窗口?
How to open File CHooser window on button press in Android app using Kotlin?
推荐答案
在您的活动中,单击按钮以添加意图:
In your activity add the button click to add an intent:
btnBack.setOnClickListener {
val intent = Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)
}
设置自定义requestCode,我设置111
Set a custom requestCode, I set 111
在活动中添加onActivityResult
以捕获结果:
Add the onActivityResult
in your activity to catch the result:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 111 && resultCode == RESULT_OK) {
val selectedFile = data?.data //The uri with the location of the file
}
}
现在selectedFile
将包含所选位置.
这篇关于使用Kotlin在Android应用程序中打开文件选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文