Android活动startActivityForResult没有被调用

时间:2019-08-05 08:15:36

标签: android android-intent android-activity kotlin

我正在尝试获取一个文件并将该文件传递给Firebase并将其存储在我的存储桶中,但是由于某些原因,即使按照建议将属性android:launchMode="singleTop"添加到清单文件后,我的startActivityForResult也没有被调用在另一个答案中,也没有在应该启动意图的按钮单击事件上使用kotlin静态函数调用。请注意,两者都处于同一活动中!

这是我的按钮单击处理程序:

override fun onCreate(savedInstanceState: Bundle?) {
   btnImage.setOnClickListener {
            val intent = Intent()
            intent.action = Intent.ACTION_GET_CONTENT
            // set the intent type
            intent.type = "image/*"
            // accept only local content
            intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true)
            startActivityForResult(Intent.createChooser(intent, "Insert Picture"), PICTURE_RESULT)
        } 
}

这是我的startAtivityForResult函数:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                val imageUri : Uri = data.data!!
                val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
                ref.putFile(imageUri)

               {


            } else {
                Log.d("IMAGE", resultCode.toString())
                Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
            }

        }
    }

2 个答案:

答案 0 :(得分:1)

  

未调用Android活动startActivityForResult

您需要在super.onActivityResult(requestCode, resultCode, data)内致电onActivityResult

您已在super.onActivityResult(requestCode, resultCode, data)内注释了onActivityResult的代码

//内的super.onActivityResult(requestCode, resultCode, data)中删除 onActivityResult

此外,使用此

 if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT)

对此内容的理解

if (resultCode == PICTURE_RESULT && resultCode == Activity.RESULT_OK)

示例代码

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == PICTURE_RESULT) {
            if (data != null) {
                val imageUri : Uri = data.data!!
                val ref : StorageReference = FirebaseUtil.storageRef!!.child(imageUri.lastPathSegment!!)
                ref.putFile(imageUri)

               {


            } else {
                Log.d("IMAGE", resultCode.toString())
                Toast.makeText(this, "Upload failure", Toast.LENGTH_LONG).show()
            }

        }
    }

答案 1 :(得分:1)

将resultCode == PICTURE_RESULT更改为: if语句中的requestCode == PICTURE_RESULT。