重启App后找不到Phonegap camera.getPicture picurl

时间:2015-01-22 09:00:58

标签: android ios cordova phonegap-plugins

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

我使用相机插件。 如果我通过插件从图书馆获得照片,我会得到皮卡。 我将URL保存在localStorage中,并在应用程序中显示带有html的图片。 这很好。

应用程序重启后,我想再次显示相同的图片,但现在picurl无效。 它与第一次相同,但现在无效。

获取照片:

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。 但它无效。

似乎该插件将图片加载到&#34; tmp&#34; -folder中,其名称类似于&#34; cdv_photo_001.jpg&#34; 如果我再次从库中放入相同的图片,我会在&#34; tmp&#34; -folder中使用文件名&#34; cdv_photo_002.jpg&#34;获得相同的网址。 是否有选项可以在设备上获取持久性URL。不是带有图片的临时文件夹。

1 个答案:

答案 0 :(得分:0)

确定。问题是,插件无法访问库。 所以这些照片进入了一个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);
    },
}