Android 通过Intent.createChooser传递附加内容

Android 通过Intent.createChooser传递附加内容,android,Android,我使用以下代码从图库中选择图片。我想从这个意图中跟踪int值 val intent = Intent() intent.type = "image/*" intent.action = Intent.ACTION_GET_CONTENT intent.putExtra("Position", 1) startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE) 但在onActivity

我使用以下代码从图库中选择图片。我想从这个意图中跟踪int值

val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
intent.putExtra("Position", 1)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE)
但在onActivityResult中尝试从intent获取传递的值时,我只获取默认值[0]

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            val selectedImageURI = data?.data
            val position = data?.getIntExtra("Position", 0)
        }
    }
}
所以我的疑问是,是否有可能通过意向选择器跟踪这些值?如果是,如何进行

是否可以通过意向选择器跟踪值

不,你的问题与选择者无关。您将获得与
startActivityForResult(意图)相同的结果,请选择图片)


startActivityForResult()
启动的活动无需向您返回您在该
Intent

中放入的任何额外内容,您必须在此行中使用
requestCode

startActivityForResult(Intent.createChooser(intent, "Select Picture"), requestCode)
// for example use SELECT_PICTURE_POSITION_1 as requestCode
然后你回到这里:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (resultCode == RESULT_OK) {
        when (requestCode) {// use your request code here
            SELECT_PICTURE_POSITION_0 -> {
                          val selectedImageURI = data?.data
                          val position = data?.getIntExtra("Position", 0)
            }
            SELECT_PICTURE_POSITION_1 -> {
                          val selectedImageURI = data?.data
                          val position = data?.getIntExtra("Position", 1)
            }
            SELECT_PICTURE_POSITION_2 -> {
                          val selectedImageURI = data?.data
                          val position = data?.getIntExtra("Position", 2)
            }
            //other conditions here
        }
    }
}
根据您的需要使用多个请求代码