使用RoboVM访问图库和相机

时间:2014-05-08 21:21:38

标签: robovm

首先,我非常感谢您对此处发布的疑问的支持和回应。

我正在开发一个需要访问图库和相机的应用程序,因为它有一个用户配置屏幕。 我想我已经深入研究了如何使用RoboVM做这些事情并发现: What is startActivityForResult analogue in RoboVM? https://groups.google.com/forum/#!topic/robovm/BX5MsTF1tws https://groups.google.com/forum/#!topic/robovm/CZR4KwTBlkg

但是当我实现它时:

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;

    }
}

当我尝试打开图库时:

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)
你能帮帮我吗?我搞不清楚了! 非常感谢先进!

祝你好运, 路易斯。

1 个答案:

答案 0 :(得分:2)

谢谢你给我看门。我设法通过为UIImagePickerControllerDelegate创建一个单独的类来解决你的异常,该类扩展了UINavigationControllerDelegateAdapter。您定义java.lang.Object扩展的匿名内部类,这就是您获得此异常的原因。因此,类定义应如下所示:

public class AppleUIImagePickerControllerDelegate extends UINavigationControllerDelegateAdapter implements UIImagePickerControllerDelegate