registerForActivityResult() 是startActivity() 的替换方法,它囊括了更多的功能回调,并且可以赋值给对象,通过 xxx.launch() 方法重复使用。
1.StartActivityForResult()
回调 it:ActivityResult!
resultCode 有三种 分别为 :
RESULT_OK,RESULT_ERROR,RESULT_CANCELED
val startActivity= registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ //首先判断resultCode if (it.resultCode == RESULT_OK){ //后续对data进行操作... //data 是 intent 类型的 it.data.getIntExtra() }else{}}
跳转activity
startActivity.launch(mIntent)
在第二个activity进行回调传参
setResult(RESULT_OK,intent)
2.权限请求
单个权限请求
val requestCameraPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean -> if (isGranted) { //做后续操作 } else { // 权限被拒绝就要提醒 } }
requestCameraPermission.launch(Manifest.permission.CAMERA)
多权限请求
注意:多权限请求 返回的是map类型参数,所以需要获取map中所有的value来进行判断是否拿到所有权限
requestMultiplePermission.launch(arrayOf(Manifest.permission.CAMERA,Manifest.permission.READ_EXTERNAL_STORAGE))val requestMultiplePermission = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { result: Map -> if (result[Manifest.permission.CAMERA] == true && result[Manifest.permission.READ_EXTERNAL_STORAGE] == true){ //权限全部获取后的操作 }else{ //没有全部获取的操作 } }
3.文件选择
需要在launch()中写入MIME Type
//获取相册图片selectFile .launch("image/*")//获取视频selectFile .launch("video/*")//获取音频selectFile .launch("audio/*")//txt类型文件selectFile .launch("text/plain")//pdf类型文件selectFile .launch("application/pdf")//Microsoft Word文件selectFile .launch("application/msword")
val selectPhoto = registerForActivityResult(ActivityResultContracts.GetContent()){result:Uri ->if (result!=null){//返回图片的uri val bitmap=BitmapFactory.decodeStream(contentResolver.openInputStream(result)) }}
注意:使用GetContent()文件选择器 是无法直接打开相册 需要用户自行选择打开 如下图所示
所以如果需要优化用户体验 则仍需使用 startActivityForResult()
例:
Intent().also { it.action=Intent.ACTION_PICK it.data=MediaStore.Images.Media.EXTERNAL_CONTENT_URI startActivityForResult(it, Content.TAKE_ALBUM_REQUEST) }
4.拍照
拍照有两个方法:
TakePicturePreview() 方法 是直接获取的相机预览界面,返回bitmap
TakePicture() 保存到本地 返回 uri
启动方法:
launch(null)
launch(uri) 照片保存路径
private val takePhoto = registerForActivityResult(ActivityResultContracts.TakePicturePreview()) { } private val takePhoto registerForActivityResult(ActivityResultContracts.TakePicture()){ }
5.创建文件
返回创建文件的uri
启动:
launch(“image.jpg”) 文件名+后缀
private val createDocument = registerForActivityResult(ActivityResultContracts.CreateDocument()) { }
6.选择联系人电话
private val pickContact = registerForActivityResult(ActivityResultContracts.PickContact()) { uri -> val cursor:Cursor?=mContext.contentResolver.query(uri,null,null,null,null) cursor?.let { cursor -> while (cursor.moveToNext()){ //联系人id val contactId:String=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)) //联系人姓名 val contactName=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)) val phones=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactId, null,null ) phones?.let { while (it.moveToNext()){ //联系人电话 val phoneNumber=it.getString(it.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) } it.close() } } cursor.close() } }
启动:在请求前需要先请求 Manifest.permission.READ_CONTACTS 权限
val requestPermission = registerForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted: Boolean -> if (isGranted) { pickContact.launch(null) } else { // 权限被拒绝就要提醒 //showSettingDialog() } }
来源地址:https://blog.csdn.net/oxygen112/article/details/126887646