0

我有一个程序允许我在执行使用 android 系统相机活动拍摄的照片期间存储数据(照片和所拍照片的元数据)......但我有代码来确保用户输入数据在使用该功能显示相机活动之前进入弹出活动OnActivityResult(这样用户的照片具有作为元数据存储在我的firebase数据库中的信息)。我想知道如果我可以设置一个不等于的请求代码,REQUESTCODE2以便在按下我的后退按钮的情况下(这仍然会导致REQUESTCODE2com.example.myapplication.nameofphoto活动返回,然后将触发takepic())我可以故意确保请求代码有问题,以便takepic()不会触发,我不会将空数据存储到我的数据库中。

供您参考:nameofpersonvarnameofphotovar都属于不同的类别,并且是来自弹出活动的信息

private const val   REQUESTCODE = 2
private const val   REQUESTCODE2 = 3

fun take_pic(){
    val takephotoIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    if (takephotoIntent.resolveActivity(this.packageManager) != null) {
        startActivityForResult(takephotoIntent, REQUESTCODE)
    } else {
        Toast.makeText(this, "Unable To access Camera... ", Toast.LENGTH_LONG)
            .show()
    }


}
        photoButton.setOnClickListener {
            val action3 = Intent(this , com.example.myapplication.nameofphoto::class.java)
            startActivityForResult(action3, REQUESTCODE2 )

    }




override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (REQUESTCODE == requestCode && resultCode == Activity.RESULT_OK) {
            //Compressing the bitmap(image) into a byte[] to match the input of the .putbytes method
            val userimage = data?.extras?.get("data") as Bitmap
            val byteoutput = ByteArrayOutputStream()
            userimage.compress(Bitmap.CompressFormat.JPEG,100 , byteoutput)
            val data = byteoutput.toByteArray()
            //ref to the firebase "bucket" database
            val storageinfo = FirebaseStorage.getInstance().getReference("/Images" )
            //extra data that shows who the images belong to (users)

            val metadatastoreage = storageMetadata {
                setCustomMetadata("Name of person" , nameofpersonvar)
                setCustomMetadata("Name of photo" , nameofphotovar)}
           storageinfo.putBytes(data, metadatastoreage)



        }else if (requestCode ==REQUESTCODE2) {
            take_pic()

        }
        else {
            super.onActivityResult(requestCode, resultCode, data)
        }
        }
4

1 回答 1

1

那你为什么不发送一些与当前打开的活动的后按方法不同的结果代码并检查结果是否成功然后选择否则做一些事情。

将此作为结果代码从后按方法发送。RESULT_CANCELED

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    if (REQUESTCODE == requestCode && resultCode == Activity.RESULT_OK) {
        //Compressing the bitmap(image) into a byte[] to match the input of the .putbytes method
        val userimage = data?.extras?.get("data") as Bitmap
        val byteoutput = ByteArrayOutputStream()
        userimage.compress(Bitmap.CompressFormat.JPEG,100 , byteoutput)
        val data = byteoutput.toByteArray()
        //ref to the firebase "bucket" database
        val storageinfo = FirebaseStorage.getInstance().getReference("/Images" )
        //extra data that shows who the images belong to (users)

        val metadatastoreage = storageMetadata {
            setCustomMetadata("Name of person" , nameofpersonvar)
            setCustomMetadata("Name of photo" , nameofphotovar)}
       storageinfo.putBytes(data, metadatastoreage)
       return
    }

   if (requestCode ==REQUESTCODE2 && resultcode == Activity.RESULT_OK) {
        take_pic()
   } else {
        //back pressed do something.
        //finish etc
    }
}

编辑:您可以覆盖弹出活动中的 onBackPressed() 并使用意图向父活动发送一些数据。例如。

Intent resultIntent = new Intent();
// TODO Add extras or a data URI to this intent as appropriate.
resultIntent.putExtra("user_pic_click", "some data");
setResult(Activity.RESULT_OK, resultIntent);
finish();
于 2020-05-25T08:31:54.933 回答