在这段代码中,onActivityForResult
永远不会被调用。
我的活动:
class PersonalInformationActivity : AppCompatActivity(), View.OnClickListener {
private val READ_REQUEST_CODE = 2
lateinit var user: User
lateinit var email: EditText
lateinit var phoneNumber: EditText
lateinit var username: EditText
lateinit var profilePicture: ImageView
lateinit var photoURI: Uri
//someCode
override fun onClick(v: View) {
when (v.id) {
R.id.profilePicture -> {
importPicture()
if (photoURI != null) {
try {
val file = FileUtil.from(this, photoURI)
RequestAddUserProfilePicture.MakeRequestTask(this, this).execute(user.token, file, photoURI.path!!.substring(photoURI.path!!.lastIndexOf("/") + 1))
Log.d("file", "File...:::: uti - " + file.path + " file -" + file + " : " + file.exists())
} catch (e: IOException) {
e.printStackTrace()
}
} else {
Toast.makeText(this, "need pic", Toast.LENGTH_LONG).show()
}
}
}
}
private fun importPicture() {
val intent = Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
setResult(Activity.RESULT_OK)
startActivityForResult(intent, READ_REQUEST_CODE)
}
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
if (data != null) {
photoURI = data.data
Log.d("URI", "Uri:" + photoURI.toString())
}
}
}
}
我已经读过这个答案:https://stackoverflow.com/a/19546302/7937498并尝试过技巧,但没有解决我的问题。
对活动和片段使用不同的REQUEST_CODES。我尝试了你的代码并尝试了不同的请求代码101和102,我得到了结果。
这不是一个真正的解决方法,但我将其从Kotlin重构为Java(完全相同的代码),它也可以...