在此代码中,从未调用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 并尝试了技巧,但未能解决我的问题。
答案 0 :(得分:1)
对活动和片段使用不同的REQUEST_CODES。我尝试了您的代码,并尝试了不同的请求代码101和102,我得到了结果。
答案 1 :(得分:0)
这不是真正的解决方法,但我将其从Kotlin重构为Java(完全相同的代码),并且效果很好...