ImagePicker icon indicating copy to clipboard operation
ImagePicker copied to clipboard

How to use this Imagepicker in fragment?

Open vtxmg opened this issue 3 years ago • 14 comments

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.

vtxmg avatar Sep 09 '21 14:09 vtxmg

Hello, I use ImagePicker in fragment without any problem. Just add requireActivity() before getExternalFilesDir

imagePicker

About this, make sure you are not using this in some callback or object

DevPraly avatar Sep 09 '21 17:09 DevPraly

@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 avatar Sep 09 '21 20:09 vtxmg

@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 avatar Sep 10 '21 07:09 DevPraly

@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 avatar Sep 11 '21 17:09 vtxmg

@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 avatar Sep 13 '21 07:09 DevPraly

@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 avatar Sep 19 '21 15:09 vtxmg

@vtxmg Have you tried override the onResume() ?

override fun onResume() {
     super.onResume()
     image.setImageURI(uri)
}

ArmandDitto avatar Oct 11 '21 21:10 ArmandDitto

i have the same issue, do you have a resolution?

khchoudhary8 avatar Nov 26 '21 21:11 khchoudhary8

does anybody have fixed this?

titosobabas avatar Jan 11 '22 05:01 titosobabas

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 avatar Jan 11 '22 08:01 PickupAgency-Admin

@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.

SopheapSK avatar Jan 14 '22 04:01 SopheapSK

@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?

Nishmitha1299 avatar Feb 26 '22 15:02 Nishmitha1299

@PickupAgency-Admin your code is working fine... thanks.

its-mr-jerry avatar Apr 25 '22 11:04 its-mr-jerry

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

Shawon-mas avatar Jun 07 '22 09:06 Shawon-mas