Home > Software design >  registerForActivityResult resultCode get 0 value
registerForActivityResult resultCode get 0 value

Time:07-01

I'm trying to use registerForActivityResult but the result of it is 0, which mean that it doesn't get the result from the activity. The code was working perfectly last month when i did it, but i don't know why it made the error today, i tried to check for error in the code, but i dont think there is one.

Here is the function to use the camera :

private fun UseCamera() {
        val takePictureIntent = Intent (MediaStore.ACTION_IMAGE_CAPTURE)
        val imagePath = File(Environment.getExternalStorageDirectory(), "Pictures")
        val photoFile = File(imagePath, "my_picture.jpg")
        FilePath = FileProvider.getUriForFile(this, FILE_AUTHORITY, photoFile )
        Log.w("FilePath",FilePath.toString())
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FilePath)
        takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
        getPreviewImage.launch(takePictureIntent)
        Log.w("UseCamera","Successful")
    }

and here is the registerForResultActivity :

        getPreviewImage = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                result -> Log.e("Preview Image", result.resultCode.toString())
            if (result.resultCode == RESULT_OK) {
                if (!forfood){
                Log.i("File Path", FilePath.toString())
                val SelectedImage = FilePath
                val PicRef = StorageRef.child(preferences.getValue("username").toString())
                    .child("kiosk_pic/"   ImageID)
                PicRef.putFile(SelectedImage).addOnSuccessListener {
                    Toast.makeText(this, "Uploaded", Toast.LENGTH_SHORT).show()
                    PicRef.downloadUrl.addOnSuccessListener {
                        preferences.setValue("kiosk_pic", it.toString())
                        ref.child(preferences.getValue("username").toString()).child("kiosk_picture").setValue(it.toString())
                        setKioskImage(preferences.getValue("kiosk_pic").toString(),ImageID)
                    }
                }
                    .addOnFailureListener {
                        Toast.makeText(this, "Upload Fail", Toast.LENGTH_SHORT).show()
                    }
                    .addOnProgressListener {
                        Toast.makeText(this, "Uploading", Toast.LENGTH_SHORT).show()
                    }
            } else {
                    Log.i("File Path",FilePath.toString())
                    val SelectedImage = FilePath
                    if (add){
                        iv_addimage.setImageURI(SelectedImage)
                    }else{
                        iv_changeimage.setImageURI(SelectedImage)
                    }
                }
        }
        }

i added the log and the result was this everytime i use the camera :

W/UseCamera: Successfull
D/OpenGLRenderer: endAllActiveAnimators on 0xea49f7f0 (AlertController$RecycleListView) with handle 0xc1acc9b0
E/Preview Image: 0

what did i do wrong here? since it worked perfectly before

EDIT and also the log says this :

W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.

CodePudding user response:

I have fix the problem by changing the UseCamera function code. im not really sure, but i think the problem was this part :

        val imagePath = File(Environment.getExternalStorageDirectory(), "Pictures")
        val photoFile = File(imagePath, "my_picture.jpg")

since im not really sure, my guess is that the temporary file that was created was missing right after the picture was taken so then make the resultcode return RESULT_CANCELED

i change my code to this:

    private fun UseCamera() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        val imageFile = getExternalFilesDir (Environment.DIRECTORY_PICTURES)
        val tempFile = File.createTempFile("my_picture",".jpg",imageFile)
        FilePath = FileProvider.getUriForFile(this, FILE_AUTHORITY,tempFile)
        Log.e("File Path", FilePath.toString())
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,FilePath)
        getPreviewImage.launch(takePictureIntent)
}

and change the provider path to this

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="picture" path="Android/data/com.example.njajal/files/Pictures"/>
</paths>

i hope my answer, help some people with the same problem

  • Related