【问题标题】:Phonegap camera.getPicture picurl not found after restart the App重启App后找不到Phonegap camera.getPicture picurl
【发布时间】:2015-01-22 18:47:06
【问题描述】:

我一直在用 Phonegap 开发一个应用程序。

我使用相机插件。 如果我通过插件从库中获取照片,我会得到 picurl。 我将 url 保存在 localStorage 中,并在应用程序中显示带有 html 的图片。 效果很好。

应用重启后,我想再次显示同一张图片,但现在图片网址无效。 和第一次一样的路径,但是现在无效了。

获取照片:

pictureSource = navigator.camera.PictureSourceType;
            destinationType = navigator.camera.DestinationType;
            navigator.camera.MediaType = 0;
            navigator.camera.getPicture(onPhotoURISuccess, onFail, {
                quality: 50,
                destinationType: destinationType.FILE_URI,
                mediaType: navigator.camera.MediaType.PICTURE,
                sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
            });
function onPhotoURISuccess(imageURI) {
            localStorage.pic = imageURI;
            $scope.$apply(function () {
                var html = '<img id="img1" src="' + imageURI + '" />';
                $scope.photogallery = html;
            }
            )
        }

出于测试目的,我在视图中添加了 picurl:

<img ng-src="picurl" />

重新启动后,我认为没有图片。 如果我在浏览器中使用检查器观看它,我会看到正确的 url。 但是无效。

该插件似乎将图片加载到名称为“cdv_photo_001.jpg”的“tmp”文件夹中 如果我再次从库中放入相同的图片,我会在“tmp”文件夹中获得相同的 url,文件名为“cdv_photo_002.jpg”。 是否可以选择在设备上获取持久 url。不是带有图片的临时文件夹。

【问题讨论】:

  • 请指定您使用的插件并分享一些代码,如果您可以分享显示url无效的错误
  • 完成。重启后Url好像变了。
  • 如果你使用angular记住img src属性不起作用,你必须使用ng-src stackoverflow.com/q/18235169/1636209
  • 好的。对不起。但这并不重要。我在我的问题中添加了更多信息。
  • 可能当应用程序重新启动时它会丢失图片,因为临时文件夹被隐藏了。检查app重启后文件夹是否包含两个文件001.jpg和002.jpg。或者只有 002.jpg,最新的。

标签: android ios cordova phonegap-plugins


【解决方案1】:

好的。问题是,插件无法访问库。 所以图片进入了一个 tmp 文件夹。

我现在的解决方案是将图片从文件夹移动到持久文件系统。

var app = {
    capturePhoto: function () {
        if (!navigator.camera) {
            alert('Camera API not supported');
        }

        navigator.camera.getPicture( app.cameraSuccess, app.cameraError, {
            quality: 50,
            destinationType: Camera.DestinationType.FILE_URI
        });

    },

    cameraSuccess: function (imageData) {
        console.log('cameraSuccess: '+imageData);

        app.movePhoto( imageData );
    },

    movePhoto: function (file){
        alert(file);
        window.resolveLocalFileSystemURI( file , app.resolveOnSuccess, app.resOnError);
    },

    resolveOnSuccess: function (entry){
        var d = new Date();
        var n = d.getTime();

        //new file name
        var newFileName = n + ".jpg";
        var myFolderApp = "my_folder";

        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {

                fileSys.root.getDirectory( myFolderApp,
                    {create:true},
                    function(directory) {
                        entry.moveTo(directory, newFileName, function(new_entry){
                            path = new_entry.fullPath;
                            url = new_entry.toURL();

                            console.log(path+"\n"+url);

                            alert( path+"\n"+url );

                            jQuery('body').append('<img src="'+path+'" />');

                        }, app.resOnError);
                    },
                    app.resOnError);
            },
            app.resOnError);
    },

    resOnError: function(error) {
        alert('Error '+error.code+': '+error.message);
    },
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 2017-06-01
    相关资源
    最近更新 更多