【问题标题】:How can I choose picture from android gallery using Nativescript?如何使用 Nativescript 从 android 图库中选择图片?
【发布时间】:2016-07-20 08:43:32
【问题描述】:

我一直在浏览 Nativescript 网站,但没有找到从手机本地获取图片的方法。如果有人知道在 android 和 ios 上都可以做到这一点,我会非常高兴。

【问题讨论】:

    标签: android nativescript angular2-nativescript


    【解决方案1】:

    在您的情况下,您可以使用 file-system module 和本机代码从您的设备访问外部存储。您可以获取文件的路径并使用 NativeScript file-system api 打开它。我附上了示例示例,您如何从 相机胶卷 存储中获取图像的路径。您还可以查看文章here,其中已经描述了您可以从 android 设备存储中访问的内容。

    打字稿

     import {path} from "file-system";
    
     var tempPicturePath = path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath(), "20160719_155053.jpg");
        console.log("extr storage "+tempPicturePath);
    

    javascript

    var file_system_1 = require("file-system");
    
    var tempPicturePath = file_system_1.path.join(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath(), "20160719_155053.jpg");
            console.log("extr storage " + tempPicturePath);
    

    【讨论】:

    【解决方案2】:

    尼克,我想你想用户从图库中选择图像,对吧?我用这个 nativescript 插件完成了这个:

    https://www.npmjs.com/package/nativescript-imagepicker

    import * as imagepicker from "nativescript-imagepicker";
    
    let context = imagepicker.create({
        mode: "single" // use "multiple" for multiple selection
    });
    
    context
        .authorize()
        .then(function() {
            return context.present();
        })
        .then(function(selection) {
            selection.forEach(function(selected) {
                // process the selected image
            });
            list.items = selection;
        }).catch(function (e) {
            // process error
        });
    

    【讨论】:

    • 我试过了,但是它打开的是文件库,而不是图片库。
    【解决方案3】:

    根据https://whatwebcando.today/camera-microphone.html,您可以只使用纯 HTML 输入字段。

    <input type="file" accept="image/*">
    

    按下移动设备上的按钮会打开包含实时相机图片选项和文件浏览器 (Anroid (Mi9T) behaviour) 的图库。

    希望这个答案不会错过您的观点;-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-02
      • 2013-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      相关资源
      最近更新 更多