Open immujahidkhan opened 1 year ago
package com.imagepicker.example.java;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.nguyenhoanglam.imagepicker.model.Image;
import com.nguyenhoanglam.imagepicker.model.ImagePickerConfig;
import com.nguyenhoanglam.imagepicker.ui.imagepicker.ImagePickerKt;
import com.nguyenhoanglam.imagepicker.ui.imagepicker.ImagePickerLauncher;
import java.util.ArrayList;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;
public class MainActivity extends AppCompatActivity {
public static class JvmContextProvider implements Function0<Context> {
@Nullable
private Activity hostAct;
@Nullable
private Fragment hostFrag;
public JvmContextProvider(@NonNull Activity host) {
this.hostAct = host;
}
public JvmContextProvider(@NonNull Fragment host) {
this.hostFrag = host;
}
@Override
public Context invoke() {
if (hostAct != null) {
return hostAct;
} else if (hostFrag != null) {
return hostFrag.requireContext();
} else {
throw new IllegalArgumentException();
}
}
}
public interface JvmImagePickerCallback extends Function1<ArrayList<Image>, Unit> {
void onImagePickerResult(ArrayList<Image> images);
@Override
default Unit invoke(ArrayList<Image> images) {
this.onImagePickerResult(images);
return null;
}
}
private final ImagePickerLauncher launcher = ImagePickerKt.registerImagePicker(
this,
new JvmContextProvider(this),
new JvmImagePickerCallback() {
@Override
public void onImagePickerResult(ArrayList<Image> images) {
// TODO handle image result
}
}
);
private final ImagePickerLauncher launcherLambda = ImagePickerKt.registerImagePicker(
this,
() -> this,
(ArrayList<Image> images) -> {
// TODO handle image result
return null;
}
);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ImagePickerConfig config = new ImagePickerConfig();
// TODO config.setSomething();
launcher.launch(config);
// or
// launcherLambda.launch(config);
}
}
@hushenghao thanks for this. Don't forget to post your answer to this stackoverflow question too, it will help a lot of people. https://stackoverflow.com/questions/78357957/is-there-any-way-to-use-nguyenhoanglamimagepicker1-6-3-in-android-java/78358374#78358374
I want to integrate this in my java class