ImagePicker icon indicating copy to clipboard operation
ImagePicker copied to clipboard

Calling ImagePciker from a Fragment

Open androXdev opened this issue 3 years ago • 19 comments

Hi Dhaval2404, I am having an issue while fetching the result of the data from ImagePciker. I am using it in java and not kotlin. As you know that onActivityResult() is deprecated in fragments so its troublesome to receive the data in it. I am able to receive the data in Activity level so I know its working! I just want to know if using ActivityResultLauncher whats the parameter i need to pass inside createIntent() of image picker for java.

ImagePicker.with(this) .compress(1024) .maxResultSize(1080, 1080) .createIntent { intent -> startForProfileImageResult.launch(intent) }

How to resolve that lamba function for java since activityResultLauncher needs an intent to fire up. I really want to implement this ImagePicker in my project since its so enticing. Any help would suffice.

androXdev avatar Jun 16 '21 14:06 androXdev

I'm having the same problem, is there a more detailed explanation for lambda

mehmet-cpu avatar Jun 23 '21 10:06 mehmet-cpu

Right after the class/fragment creation, where you usually create the private vars of the fragment, put this:

private val startForProfileImageResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
            val resultCode = result.resultCode
            val data = result.data

            when (resultCode) {
                Activity.RESULT_OK -> {
                    //Image Uri will not be null for RESULT_OK
                    val fileUri: Uri = data?.data!!
                    // HERE YOU USE THE FILEURI AS A URI OBJECT TO SET A IMAGE, SIMPLE!
                }
                ImagePicker.RESULT_ERROR -> {
                    Toast.makeText(YourContext, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
                }
                else -> {
                    Toast.makeText(YourContext, "Task Cancelled", Toast.LENGTH_SHORT).show()
                }
            }
        }

anderbytes avatar Jul 07 '21 14:07 anderbytes

Thank you anderbytes, There is no problem in the codes you write to get results. I'm having a problem with Java, not Kotlin, for example when creating an intent. " .createIntent { intent -> startForProfileImageResult.launch(intent)} " I had a problem executing it as java code.

mehmet-cpu avatar Jul 07 '21 20:07 mehmet-cpu

You mean that you only need that code but working on Java? If so... have you already tried some converter or anything?

anderbytes avatar Jul 07 '21 22:07 anderbytes

No, I haven't tried, but I prefer to use ImagePciker in activity instead of fragment, I couldn't succeed in fragment.

mehmet-cpu avatar Jul 07 '21 23:07 mehmet-cpu

Yes I'm just doing it on Java

mehmet-cpu avatar Jul 08 '21 00:07 mehmet-cpu

ImagePicker.with(this)
                .crop()
                .compress(1024)
                .createIntent(intent -> {
                     imagePickerResultLauncher.launch(intent);
                });

I got the same problem too on Java. Missing return statement. Please kindly help.

nguyencongbinh avatar Jul 09 '21 08:07 nguyencongbinh

Did any of you find a solution yet?

nalamzap avatar Jul 27 '21 07:07 nalamzap

I couldn't find any solution

mehmet-cpu avatar Jul 27 '21 14:07 mehmet-cpu

This works for me.

ImagePicker.Builder with = ImagePicker.with(this);
       with.crop();
       with.compress(1024);
       with.maxResultSize(1080, 1080);
       with.createIntent(new Function1<Intent, Unit>() {
           @Override
           public Unit invoke(Intent Intent) {
               startForProfileImageResult.launch(Intent );
               return null;
           }
       });

alankko avatar Jul 28 '21 12:07 alankko

Thank you alankko :) I tested it and it works fine

mehmet-cpu avatar Jul 28 '21 15:07 mehmet-cpu

where is the java code for private val startForProfileImageResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult -> val resultCode = result.resultCode val data = result.data

        when (resultCode) {
            Activity.RESULT_OK -> {
                //Image Uri will not be null for RESULT_OK
                val fileUri: Uri = data?.data!!
                // HERE YOU USE THE FILEURI AS A URI OBJECT TO SET A IMAGE, SIMPLE!
            }
            ImagePicker.RESULT_ERROR -> {
                Toast.makeText(YourContext, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
            }
            else -> {
                Toast.makeText(YourContext, "Task Cancelled", Toast.LENGTH_SHORT).show()
            }
        }
    }

Briandosha avatar Sep 06 '21 09:09 Briandosha

Briandosha THÄ°S Ä°S THE JAVA CODE for getting data on fragment

https://developer.android.com/training/basics/intents/result#java

ActivityResultLauncher<Intent> mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == Activity.RESULT_OK) { Intent intent = result.getData(); if (intent != null) {

                            }
                        }
                    });

mehmet-cpu avatar Sep 07 '21 10:09 mehmet-cpu

does anybody has the final solution to this problem? i'm facing the same trouble, i can't fetch the result within a fragment using JAVA (not kotlin)

titosobabas avatar Jan 11 '22 05:01 titosobabas

Hi titosobabas, solution is this;

ImagePicker.Builder with = ImagePicker.with(this); with.crop(); with.compress(1024); with.maxResultSize(1080, 1080); with.createIntent(new Function1<Intent, Unit>() { @Override public Unit invoke(Intent Intent) { startForProfileImageResult.launch(Intent ); return null; } });

ActivityResultLauncher mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == Activity.RESULT_OK) { Intent intent = result.getData(); if (intent != null) {

                }
            }
        });

mehmet-cpu avatar Jan 11 '22 05:01 mehmet-cpu

Thanks @mehmet-cpu

I have a little question. Where do i should tu put the last slice of code you wrote me? I'm asking because i was trying to put those lines in my beginning fragment class but everything is in red.

I'm sorry but i would to know if am i doing something wrong.

Thanks again.

titosobabas avatar Jan 11 '22 06:01 titosobabas

Thanks @mehmet-cpu

I have a little question. Where do i should tu put the last slice of code you wrote me? I'm asking because i was trying to put those lines in my beginning fragment class but everything is in red.

I'm sorry but i would to know if am i doing something wrong.

Thanks again.

//use codes like this********************************************************

private void goGalleryOrCamera() { ImagePicker.Builder with = ImagePicker.with(this); with.crop(); with.compress(1024); with.maxResultSize(1080, 1080); with.createIntent(new Function1<Intent, Unit>() { @OverRide public Unit invoke(Intent Intent) { startForProfileImageResult.launch(Intent ); return null; } }); }

ActivityResultLauncher mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent intent = result.getData();
                if (intent != null) {

                }
            }
        });

mehmet-cpu avatar Jan 11 '22 06:01 mehmet-cpu

Thanks @mehmet-cpu I have a little question. Where do i should tu put the last slice of code you wrote me? I'm asking because i was trying to put those lines in my beginning fragment class but everything is in red. I'm sorry but i would to know if am i doing something wrong. Thanks again.

//use codes like this********************************************************

private void goGalleryOrCamera() { ImagePicker.Builder with = ImagePicker.with(this); with.crop(); with.compress(1024); with.maxResultSize(1080, 1080); with.createIntent(new Function1<Intent, Unit>() { @OverRide public Unit invoke(Intent Intent) { startForProfileImageResult.launch(Intent ); return null; } }); }

ActivityResultLauncher mStartForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == Activity.RESULT_OK) {
                Intent intent = result.getData();
                if (intent != null) {

                }
            }
        });

Hi may I ask if you know why for me it says "cannot access Function1 with.createIntent( ^ class file for kotlin.jvm.functions.Function1 not found" I'm also confused what is the Unit class I'm getting error with that as well. Any help is greatly appreciated!

monfreign avatar Jan 16 '22 13:01 monfreign

@monfreign hi bro, I don't quite understand why you are getting such an error. If you are preparing a project with java, you can get uri results by accessing the gallery and camera by using these codes in the fragment, I use these codes myself in the fragment and do not get an error. You said: "Cannot find class file for kotlin.jvm.functions.Function1" Also I am confused about what Unit class is and I am getting error on that too." Latest version of Imagepicker is designed entirely on kotlin so you can use these codes to use in java but sorry i don't know why finction1 and unit are used.

mehmet-cpu avatar Jan 17 '22 07:01 mehmet-cpu