Francesco Galgani
Francesco Galgani

Reputation: 6249

Get video file paths by iOS Gallery: workaround for a bug?

Since currently on Codename One iOS apps, the Gallery doesn't return usable video file paths, because of the bug:

https://github.com/codenameone/CodenameOne/issues/3181

which causes are probably explained here:

didFinishPickingMediaWithInfo returns different URL in iOS 13

and since the gallery is a basic crucial feature that I cannot avoid to use, I tried to workaround this issue with a temporary native interface, until the bug will be properly fixed.

I tried to copy the code from this answer: https://stackoverflow.com/a/31355092/1277576

writing the following files:

Gallery.java

package myapp.utilities;

import com.codename1.system.NativeLookup;

/**
 * Gallery
 */
public class Gallery {
    private static GalleryNative nativeInterface = NativeLookup.create(GalleryNative.class);
    
    public static String pickVideo() {
        if (nativeInterface != null && nativeInterface.isSupported()) {
            return nativeInterface.pickVideo();
        } else {
            return null;
        }
    }
} 

GalleryNative.java

package myapp.utilities;

import com.codename1.system.NativeInterface;

/**
 * @deprecated
 */
public interface GalleryNative extends NativeInterface {
    
    public String pickVideo();

} 

GalleryNativeImpl.h

#import <Foundation/Foundation.h>

@interface myapp_utilities_GalleryNativeImpl : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
}

-(NSString*)pickVideo;
-(BOOL)isSupported;
@end

GalleryNativeImpl.m

#import "myapp_utilities_GalleryNativeImpl.h"
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h> // needed for video types

@implementation app_aren_client_utilities_GalleryNativeImpl : UIViewController

-(NSString*)pickVideo{
    dispatch_sync(dispatch_get_main_queue(), ^{
        // Present videos from which to choose
        UIImagePickerController *videoPicker = [[UIImagePickerController alloc] init];
        videoPicker.delegate = self; // ensure you set the delegate so when a video is chosen the right method can be called

        videoPicker.modalPresentationStyle = UIModalPresentationCurrentContext;
        // This code ensures only videos are shown to the end user
        videoPicker.mediaTypes = @[(NSString*)kUTTypeMovie, (NSString*)kUTTypeAVIMovie, (NSString*)kUTTypeVideo, (NSString*)kUTTypeMPEG4];

        videoPicker.videoQuality = UIImagePickerControllerQualityTypeHigh;
        [self presentViewController:videoPicker animated:YES completion:nil];
    });
    return nil;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    // This is the NSURL of the video object
    NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];

    NSLog(@"VideoURL = %@", videoURL);
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
}


-(BOOL)isSupported{
    return YES;
}

@end

but when I call Gallery.pickVideo(), I get:

2020-07-25 16:38:08.401585+0200 MainClass[2731:207000] Warning: Attempt to present <UIImagePickerController: 0x107035800> on <app_aren_client_utilities_GalleryNativeImpl: 0x105f168d0> whose view is not in the window hierarchy! 2020-07-25 16:38:08.402068+0200 MainClass[2731:207198] [AXRuntimeCommon] Unknown client: MainClass

So my question is what I can do to get video file paths by the Gallery (inside a Codename One app, of course). Thank you

Upvotes: 1

Views: 48

Answers (1)

Shai Almog
Shai Almog

Reputation: 52770

You need to use something like this:

[[CodenameOne_GLViewController instance] presentViewController:activityViewController animated:YES completion:^{}];

As shown here: https://github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/nativeSources/IOSNative.m#L7244

You will also need this import:

#import "CodenameOne_GLViewController.h"

Upvotes: 1

Related Questions