4

First of all I appreciate so much your support and responses to the doubts posted here.

I am developing an App that needs access to image gallery and camera because it has a user configuration screen. I believe I have research well enough about how to do accomplish something like this with RoboVM and found: What is startActivityForResult analogue in RoboVM? https://groups.google.com/forum/#!topic/robovm/BX5MsTF1tws https://groups.google.com/forum/#!topic/robovm/CZR4KwTBlkg

But when I implement that:

ImagePicker.java:

package com.demo.app.interfaces;
public interface ImagePicker {
    /**
     * Open cam
     */
    void startPreview();
    /**
     * Return bytes
     * @return
     */
    byte[] getBytes();
    
    /**
     * Event after saved
     */
    void pictureJustSaved();
    /** Opens gallery
    void openGallery();
}

MyImagePickerController.java:

package com.demo.app;
import org.robovm.apple.uikit.UIImagePickerController;
import org.robovm.apple.uikit.UIInterfaceOrientation;
public class MyImagePickerController extends UIImagePickerController {
    public boolean shouldAutorotate() {
        return false;
    }
    public UIInterfaceOrientation getPreferredInterfaceOrientation() {
        return UIInterfaceOrientation.Portrait;
    }
}

IOSLauncher.java:

package com.demo.app;
import org.robovm.apple.foundation.NSAutoreleasePool;
import org.robovm.apple.foundation.NSDictionary;
import org.robovm.apple.foundation.NSString;
import org.robovm.apple.uikit.UIApplication;
import org.robovm.apple.uikit.UIImage;
import org.robovm.apple.uikit.UIImagePickerController;
import org.robovm.apple.uikit.UIImagePickerControllerDelegate;
import org.robovm.apple.uikit.UIImagePickerControllerSourceType;
import org.robovm.apple.uikit.UIInterfaceOrientation;
import org.robovm.apple.uikit.UINavigationController;
import org.robovm.apple.uikit.UINavigationControllerOperation;
import org.robovm.apple.uikit.UIViewController;
import org.robovm.apple.uikit.UIViewControllerAnimatedTransitioning;
import org.robovm.apple.uikit.UIViewControllerInteractiveTransitioning;
import org.robovm.objc.ObjCObject;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.iosrobovm.IOSApplication;
import com.badlogic.gdx.backends.iosrobovm.IOSApplicationConfiguration;
import com.demo.app.interfaces.ImagePicker;
import com.demo.app.screens.App;
public class IOSLauncher extends IOSApplication.Delegate implements ImagePicker {
    byte[] byteArray = null;
    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        return new IOSApplication(new App(this, null, null, null), config);
    }
    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.close();
    }
    @Override
    public void openGallery() {
        System.out.println("I am here: Open Gallery iOS");
        // Gallery
        UIViewController controller = ((IOSApplication) Gdx.app).getUIViewController();
        MyImagePickerController imagePicker = new MyImagePickerController();
        controller.addChildViewController(imagePicker);
        controller.getView().addSubview(imagePicker.getView());
        imagePicker.getView().setBounds(controller.getView().getBounds());
        imagePicker.setSourceType(UIImagePickerControllerSourceType.PhotoLibrary);
        UIImagePickerControllerDelegate delegate = new UIImagePickerControllerDelegate(){
            @Override
            public void willShowViewController(
                    UINavigationController navigationController,
                    UIViewController viewController, boolean animated) {
                // TODO Auto-generated method stub  
            }
            @Override
            public void didShowViewController(
                    UINavigationController navigationController,
                    UIViewController viewController, boolean animated) {
                // TODO Auto-generated method stub
            }
            @Override
            public long getSupportedInterfaceOrientations(
                    UINavigationController navigationController) {
                // TODO Auto-generated method stub
                return 0;
            }
            @Override
            public UIInterfaceOrientation getPreferredInterfaceOrientation(
                    UINavigationController navigationController) {
                // TODO Auto-generated method stub
                return null;
            }
            @Override
            public UIViewControllerInteractiveTransitioning getInteractionController(
                    UINavigationController navigationController,
                    UIViewControllerAnimatedTransitioning animationController) {
                // TODO Auto-generated method stub
                return null;
            }
            @Override
            public UIViewControllerAnimatedTransitioning getAnimationController(
                    UINavigationController navigationController,
                    UINavigationControllerOperation operation,
                    UIViewController fromVC, UIViewController toVC) {
                // TODO Auto-generated method stub
                return null;
            }
            @Override
            public void didFinishPickingImage(UIImagePickerController picker,
                    UIImage image, NSDictionary<NSString, ?> editingInfo) {
                // TODO Auto-generated method stub  
            }
            @Override
            public void didFinishPickingMedia(UIImagePickerController picker,
                    NSDictionary<NSString, ?> info) {
                // TODO Auto-generated method stub              
            }
            @Override
            public void didCancel(UIImagePickerController picker) {
                // TODO Auto-generated method stub              
            }};
        imagePicker.setDelegate(delegate);
        imagePicker.addStrongRef((ObjCObject) delegate);
    }
    @Override
    public void startPreview() {
        // Cam
    }
    @Override
    public byte[] getBytes() {
        // TODO Auto-generated method stub
        return byteArray;
    }
    @Override
    public void pictureJustSaved() {
        byteArray = null;

    }
}

And when I try to open Gallery:

Folder on...
I am here: Open Gallery iOS
Exception in thread "main" java.lang.ClassCastException: com.demo.app.IOSLauncher$1 cannot be cast to org.robovm.objc.ObjCObject
    at org.robovm.objc.ObjCObject$Marshaler.protocolToNative(ObjCObject.java)
    at org.robovm.apple.uikit.UIImagePickerController.$m$super$setDelegate$(Native Method)
    at org.robovm.apple.uikit.UIImagePickerController.setDelegate(UIImagePickerController.java)
    at com.demo.app.IOSLauncher.openGallery(IOSLauncher.java)
    at com.demo.app.screens.KidsManagerScreen$8.touchUp(KidsManagerScreen.java)
    at com.badlogic.gdx.scenes.scene2d.InputListener.handle(InputListener.java)
    at com.badlogic.gdx.scenes.scene2d.Stage.touchUp(Stage.java)
    at com.badlogic.gdx.backends.iosrobovm.IOSInput.processEvents(IOSInput.java)
    at com.badlogic.gdx.backends.iosrobovm.IOSGraphics.draw(IOSGraphics.java)
    at com.badlogic.gdx.backends.iosrobovm.IOSGraphics$1.draw(IOSGraphics.java)
    at org.robovm.apple.uikit.UIView.$cb$drawRect$(UIView.java)
    at org.robovm.apple.uikit.UIApplication.main(Native Method)
    at org.robovm.apple.uikit.UIApplication.main(UIApplication.java)
    at com.demo.app.IOSLauncher.main(IOSLauncher.java)

Could you help me please? I am lost!

ouflak
  • 2,458
  • 10
  • 44
  • 49

1 Answers1

3

Thank you for showing me the door. I managed to solve your exception by creating a separate class for the UIImagePickerControllerDelegate that extends off UINavigationControllerDelegateAdapter. The anonymous inner class you define extend of java.lang.Object which is why you get this exception. So, class definition should look like this:

public class AppleUIImagePickerControllerDelegate extends UINavigationControllerDelegateAdapter implements UIImagePickerControllerDelegate 
Christo Smal
  • 615
  • 5
  • 16