ImagePicker
ImagePicker copied to clipboard
How to use this Imagepicker in fragment?
Please give some example code to use imagepicker in fragment class. I got error:
ImagePicker.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080) //Final image resolution will be less than 1080 x 1080(Optional)
.saveDir(getExternalFilesDir(Environment.DIRECTORY_PICTURES))
.start();
I got error on (this) that means it is not recognizing (this) and also getExternalFilesDir(). I think it is because we have to pass activity but we are in fragment.
Hello, I use ImagePicker in fragment without any problem. Just add requireActivity() before getExternalFilesDir
About this, make sure you are not using this in some callback or object
@DevPraly I have used this method on activity to set image preview:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
image = findViewById(R.id.image);
uri = data.getData();
image.setImageURI(uri);
}
How to do this in fragment? Should I just start extracting img uri and set right after above code or ?
I mean I want to select image first then after selecting the image that same image should be set on imageview this was done using onActivityResult() method before. Now I have used imagepicker in fragment.
@vtxmg You can use onActivityResult method in fragment too. It's currently deprecated but still working fine. Also, check if resultCode equals Activity.RESULT_OK or ImagePicker.RESULT_ERROR before processing, and yes, you can assign image URI here
@DevPraly @Dhaval2404 I have used below code in my fragment but didn't work.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
image = getView().findViewById(R.id.image);
uri = data.getData();
image.setImageURI(uri);
}
Used imagepicker in onCreateView()
ImagePicker.with(requireActivity())
.crop()
.compress(1024)
.maxResultSize(1080, 1080)
.saveDir(requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES))
.start();
It opens the gallery and we can choose the image but won't be shown on imageview that means I think onActivityReault() is not working right? Please help me to solve this.
@vtxmg Use this as parameter for .with() Use ImagePicker in onViewCreated() instead of onCreateView() Use a private ImageView myImageView in your fragment and bind it onViewCreated
private ImageView myImageView;
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); myImageView = view.findViewById(R.id.image); ImagePicker.with(this) .crop() .compress(1024) .maxResultSize(1080, 1080) .saveDir(requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES)) .start(); }
and use myImageView in fragment's onActivityResult()
@DevPraly @Dhaval2404 your above code is not working for me. Still can't set image on imageview.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uri = data.getData();
image.setImageURI(uri);
}
@vtxmg Have you tried override the onResume() ?
override fun onResume() {
super.onResume()
image.setImageURI(uri)
}
i have the same issue, do you have a resolution?
does anybody have fixed this?
It works fine in my app.
Config:
implementation 'com.github.dhaval2404:imagepicker:2.1'
compileSdkVersion 31
buildToolsVersion "31.0.0"
In your fragment
Create an activity result launcher:
private final ActivityResultLauncher<Intent> startForMediaPickerResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
Intent data = result.getData();
if (data != null && result.getResultCode() == Activity.RESULT_OK) {
Uri resultUri = data.getData();
else {
Toast.makeText(requireActivity(), ImagePicker.getError(data), Toast.LENGTH_SHORT).show();
}
});
Pick image
private void pickMedia() {
String[] mimeTypes = {"image/png", "image/jpg", "image/jpeg"};
ImagePicker.Companion.with(this)
.saveDir(requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES))
.galleryOnly()
.galleryMimeTypes(mimeTypes)
.crop()
.compress(768)
.maxResultSize(800, 800)
.createIntent(intent -> {
startForMediaPickerResult.launch(intent);
return null;
});
}
@PickupAgency-Admin your code work just fine, but do you notice this? there is a gap between Choose Image(When its size is big) then auto compress then show it , how do you solve this? for me, I add loading, but it has one bug on there, because there is No Callback when User Click on Back Button.
@DevPraly I have also used the same code line for image capture using camera but it is not working. Can you please help me out?
@PickupAgency-Admin your code is working fine... thanks.
It works fine in my app.
Config:
implementation 'com.github.dhaval2404:imagepicker:2.1' compileSdkVersion 31 buildToolsVersion "31.0.0"
In your fragment
Create an activity result launcher:
private final ActivityResultLauncher<Intent> startForMediaPickerResult = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { Intent data = result.getData(); if (data != null && result.getResultCode() == Activity.RESULT_OK) { Uri resultUri = data.getData(); else { Toast.makeText(requireActivity(), ImagePicker.getError(data), Toast.LENGTH_SHORT).show(); } });
Pick image
private void pickMedia() { String[] mimeTypes = {"image/png", "image/jpg", "image/jpeg"}; ImagePicker.Companion.with(this) .saveDir(requireActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES)) .galleryOnly() .galleryMimeTypes(mimeTypes) .crop() .compress(768) .maxResultSize(800, 800) .createIntent(intent -> { startForMediaPickerResult.launch(intent); return null; }); }
It's work fine .Thanks for your work