任务:上传用户可以从设备中选择的图像。
我怎样才能打开文件选择器 https://en.wikipedia.org/wiki/File_dialog使用 Kotlin 在 Android 应用程序中按下按钮时出现窗口?
在您的活动中,添加按钮单击以添加意图:
btnBack.setOnClickListener {
val intent = Intent()
.setType("*/*")
.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)
}
设置自定义requestCode,我设置的111
.
Add the onActivityResult
在您的活动中捕获结果:
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
}
}
Now selectedFile
将包含他们选择的位置。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)