【发布时间】:2019-05-04 19:50:45
【问题描述】:
在这段代码中,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 并尝试了技巧,但没有解决我的问题。
【问题讨论】:
-
致电
getActivity().startActivityForResult(intent, READ_REQUEST_CODE); -
PersonalInformationActivity是否包含任何 Fragment? -
@MuhammadMuzammilSharif 我调用了 this.startActivityForResult(intent, READ_REQUEST_CODE) 但它不起作用
-
@BachVu 一点也不,这是一个非常简单的活动。
-
检查结果代码
resultCode == activity.RESULT_OK。应该是resultCode == Activity.RESULT_OK
标签: android kotlin onactivityresult