skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. if any one using java so use it
    define first it to oncreate method :

     ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
           Log.d("Capture_img", ": "+result.getData());
           if (result.getResultCode()==Activity.RESULT_OK){
               Intent intent = result.getData();
               Uri uri = Objects.requireNonNull(intent).getData();
               String path = uri.getPath();
               File ff = new File("" +path);
               String  pffImage = ff.getName();
               String path111 =  GloabalMethods.imageCompressor(getContext(), path,pffImage);
               File user_img = new File(""+path111);
               Bitmap myBitmap = BitmapFactory.decodeFile(user_img.getAbsolutePath());
               ByteArrayOutputStream bytes = new ByteArrayOutputStream();
               myBitmap.compress(Bitmap.CompressFormat.JPEG,80,bytes);
           binaryImage = Base64.encodeToString(bytes.toByteArray(),Base64.DEFAULT);
               Log.d("RRRRRRRRRRRRRRRR", "onActivityResult: "+uri);
               capture_image.setImageBitmap(myBitmap);
               capture_image.setTag("yes");
           } after that call 
     ImagePicker.with(requireActivity()) .crop() //Crop image(Optional), Check Customization for more option .compress(1024)
    //Final image size will be less thanMB(Optional) 
    .maxResultSize(1080, 1080).createIntent(intent -> { mStartForResult.launch(intent); return null; } );
    

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search