【问题标题】:The method setNativeSource() expects android.graphics.Bitmap instance方法 setNativeSource() 需要 android.graphics.Bitmap 实例
【发布时间】:2020-01-03 10:05:20
【问题描述】:

在我的应用程序中,如果我从图库中选择一张图片,我必须被重定向到带有该图片的模式窗口。问题是图像从服务下载到组件,但没有显示,而是出现错误消息。

我向图库发送请求的组件:

        const imageAsset = await this.gallery.selectPhoto();
        this.rediresct("loading");
        const options: ModalDialogOptions = {
            context: imageAsset,
            viewContainerRef: this.vref,
            fullscreen: false
        };
        setTimeout(async () => {
            const response = await this.modal.showModal(
                ConfirmImageComponent,
                options
            );
            if (response == "success") {
            }
        });
    }

服务:

    
    source: any;
    constructor() {}
    async selectPhoto(): Promise<Array<any>> {
        let context = imagepicker.create({
            mode: "single"
        });
        await context.authorize();
        let imageAsset = [];
        const selection = await context.present();
      

        imageAsset = selection;
        return imageAsset;
    }
}

从服务中下载图片并显示的组件:

    obraz: ImageAsset;
    constructor(
        private gallery: GalleryService,
        private params: ModalDialogParams
    ) {
        this.image = params.context;

        var pict = new Image();
        pict.src = this.image;
        console.log(pict.src);
    }

    ngOnInit() {}
} 

在最后的代码中,我使用控制台日志来显示选定的图像源,它可以工作,但没有显示图像。在 VS Code 中,我看到此错误:

【问题讨论】:

  • selection必须是数组,试试imageAsset = selection[0]
  • @Manoj 它可以工作,但是在这个构造中imageAsset = selection[0] 我看到一个错误:TS2740:类型'ImageAsset'缺少来自类型'any[]'的以下属性:长度,流行、push、concat 等 25 个。 我怎样才能摆脱它?在constructor(){} 上面我添加了imageAsset = [] 但错误仍然存​​在。
  • 改变返回类型,在数组类型中只放any而不是any。
  • 当我将返回类型更改为:` async selectPhoto(): Promise>` 到 ` async selectPhoto(): Promise` 应用程序不起作用并返回相同的错误。
  • 请分享一个游乐场样本。

标签: android typescript nativescript


【解决方案1】:

要返回您可以执行的单个资产,

async selectPhoto(): Promise<any> {
    let context = imagepicker.create({
        mode: "single"
    });
    await context.authorize();
    const selection = await context.present();
    return selection[0];
}

确保从选择的照片中发现错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    相关资源
    最近更新 更多