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