ImagePicker
ImagePicker copied to clipboard
Calling ImagePciker from a Fragment
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.
I'm having the same problem, is there a more detailed explanation for lambda
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()
}
}
}
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.
You mean that you only need that code but working on Java? If so... have you already tried some converter or anything?
No, I haven't tried, but I prefer to use ImagePciker in activity instead of fragment, I couldn't succeed in fragment.
Yes I'm just doing it on Java
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.
Did any of you find a solution yet?
I couldn't find any solution
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;
}
});
Thank you alankko :) I tested it and it works fine
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 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) {
}
}
});
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)
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) {
}
}
});
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.
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) {
}
}
});
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 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.