Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fragment not attached to a context #356

Closed
anupdey99 opened this issue May 5, 2021 · 9 comments
Closed

Fragment not attached to a context #356

anupdey99 opened this issue May 5, 2021 · 9 comments
Assignees

Comments

@anupdey99
Copy link

Expected Behavior

Should open imager picker from fragment

Actual Behavior

Fragment h{9e363cc} (782f797a-94b7-4939-b68b-a5a1b80e4374) not attached to a context.
App crashed

Steps to Reproduce the Problem

  1. register lister in a fragment
    val launcher = registerImagePicker { imageList: List -> }
  2. navigate to the fragment (not using nav component here)

Specifications

  • Image Picker Version: 3.0.0-beta1
  • Android OS: 10/11
  • Phone: mi Note 7 pro
@pesjak
Copy link

pesjak commented May 5, 2021

Same problem. Navigation from fragment to fragment and then immediate crash.
androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment com.ui.images.fragment: calling Fragment constructor caused an exception

and (415e9f03-c85b-432f-86d3-c007690ba0c9) not attached to a context.

It works if you put registerImagePicker in your activity and not fragment.

@anupdey99
Copy link
Author

Any luck with the issue?. In Android OS: 11 Camera only also does not work. App crash after taking picture.
My deal line is close.

@anupdey99
Copy link
Author

For temp solution I am using method in fragment

val intent = createImagePickerIntent(requireContext(), config)
startActivityForResult(intent, IpCons.RC_IMAGE_PICKER)

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
Log.d("debugImagePicker","imagePicker $requestCode $resultCode")
if (resultCode == Activity.RESULT_OK && requestCode == IpCons.RC_IMAGE_PICKER && data != null) {
val imageList = ImagePicker.getImages(data) ?: emptyList()
if (imageList.isNotEmpty()) {
val pathList = imageList.map { it.path }
Log.d("debugImagePicker", "pathList $pathList")
}
return
}
super.onActivityResult(requestCode, resultCode, data)
}

@audiserg
Copy link

private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
when (result.resultCode) {
Activity.RESULT_OK -> {
.....
}
}
}
.......
launcher.launch(createImagePickerIntent(requireContext(), getImagePickerConfig()))

@esafirm
Copy link
Owner

esafirm commented Jun 19, 2021

This should be fixed

@esafirm esafirm closed this as completed Jun 19, 2021
@leesgit
Copy link

leesgit commented Aug 11, 2021

temporary use this code

private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        val images = ImagePicker.getImages(it.data) ?: emptyList()
        val firstImage = images.firstOrNull()

        firstImage?.uri.let { uri->
            Glide.with(binding.yourImageView)
                .load(uri)
                .into(binding.yourImageView)
        }
    }
//when launch
launcher.launch(createImagePickerIntent(requireContext(), ImagePickerConfig {your config))

@kldMohammed
Copy link

kldMohammed commented Dec 28, 2021

Any solution for this problem?

I use version '2.2.0'
it works fine but has some problems in some devices with android 10+
in my device POCO M3 or emulator with android 10 images in gallery do not appears and I can't select any image

@kldMohammed
Copy link

temporary use this code

private val launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
        val images = ImagePicker.getImages(it.data) ?: emptyList()
        val firstImage = images.firstOrNull()

        firstImage?.uri.let { uri->
            Glide.with(binding.yourImageView)
                .load(uri)
                .into(binding.yourImageView)
        }
    }
//when launch
launcher.launch(createImagePickerIntent(requireContext(), ImagePickerConfig {your config))

I tried also same exceptions

@VictorAlbertos
Copy link

Declare a top level lateinit reference to the ImagePickerLauncher, and then initialise it during the onAttach callback Fragment.

class SignUpFragment : Fragment() {
    private lateinit var imagePickerLauncher: ImagePickerLauncher
    
    override fun onAttach(context: Context) {
        super.onAttach(context)
        imagePickerLauncher = registerImagePicker {
            
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants