通过 iOS Gallery 获取视频文件路径:bug 的解决方法?

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

由于目前在 Codename One iOS 应用程序上,图库没有 return 可用的视频文件路径,因为错误:

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

这里可能解释了哪些原因:

didFinishPickingMediaWithInfo returns different URL in iOS 13

由于画廊是我无法避免使用的基本关键功能,我尝试使用临时本机界面解决此问题,直到错误得到妥善修复。

我试图从这个答案中复制代码:

写入以下文件:

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

但是当我调用 Gallery.pickVideo() 时,我得到:

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

所以我的问题是如何通过图库获取视频文件路径(当然是在 Codename One 应用程序中)。谢谢

你需要使用这样的东西:

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

如图所示:https://github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/nativeSources/IOSNative.m#L7244

您还需要导入:

#import "CodenameOne_GLViewController.h"