Cordova - 将图像从网址保存到设备照片库中

时间:2015-04-14 01:58:39

标签: cordova mobile

我正在使用 Apache Cordova 开发一个下载并保存图片的应用程序,但是我无法保存并显示图库,图像会转到file:///data/data试图在Android上运行,我该怎么办?

我的代码:

 function download(URL, Folder_Name, File_Name) {
        //step to request a file system 
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);

        function fileSystemSuccess(fileSystem) {
            var download_link = encodeURI(URL);
            var ext = download_link.substring(download_link.lastIndexOf('.') + 1); //Get extension of URL
            var directoryEntry = fileSystem.root; // to get root path of directory
            directoryEntry.getDirectory(Folder_Name, { create: true, exclusive: false }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
            var rootdir = fileSystem.root;
            var fp = rootdir.toURL(); // Returns Fulpath of local directory
            console.log(rootdir);
            fp = fp + "/" + Folder_Name + "/" + File_Name; // fullpath and name of the file which we want to give
            // download function call                
            filetransfer(download_link, fp);
        }

        function onDirectorySuccess(parent) {
            //alert("Sucesso");
        }

        function onDirectoryFail(error) {
            //Error while creating directory
            alert("Unable to create new directory: " + error.code);
        }

        function fileSystemFail(evt) {
            //Unable to access file system
            alert(evt.target.error.code);
        }
    }

    function filetransfer(download_link, fp) {
        var fileTransfer = new FileTransfer();
        console.log(fp);
        // File download function with URL and local path
        fileTransfer.download(download_link, fp,
                            function (entry) {
                                //alert("download complete: " + entry.fullPath);
                            },
                         function (error) {
                             //Download abort errors or download failed errors
                             console.log(error);
                             alert(error.exception);
                             alert("download error source " + error.source);
                             //alert("download error target " + error.target);
                             //alert("upload error code" + error.code);
                         }
                    );
    }

2 个答案:

答案 0 :(得分:2)

图像正在保存,但需要Media Scanner索引图库

上的图像

媒体扫描仪:

  

MediaScannerConnection为应用程序提供了一种新传递方式   创建或下载媒体文件到媒体扫描仪服务。该   媒体扫描程序服务将从文件中读取元数据并添加   将文件发送给媒体内容提供商。

如何使用Apache Cordova / PhoneGap它没有提供本机方法来刷新本机库中的图像,我不得不寻找一个插件来完成这项工作。我找到的插件是:

cordova-mediascanner-plugin
MediaScannerPlugin

两者都有基本文档,但我使用 cordova-mediascanner-plugin

通过实现此插件,只修改了我的filetransfer方法

function filetransfer(download_link, fp) {
        var fileTransfer = new FileTransfer();
        console.log(fp);

        // File download function with URL and local path
        fileTransfer.download(download_link, fp,
                            function (entry) {
                                //alert("download complete: " + entry.fullPath);
                                window.plugins.scanmedia.scanFile(fp, function (msg) {
                                    alert("Success ScanMedia");
                                }, function (err) {
                                    alert("Fail ScanMedia: " + err);
                                })
                            },
                         function (error) {
                             //Download abort errors or download failed errors
                             console.log(error);
                             alert(error.exception);
                             alert("download error source " + error.source);
                             //alert("download error target " + error.target);
                             //alert("upload error code" + error.code);
                      }
                    );

    }

答案 1 :(得分:1)

请尝试在您的项目中导入此插件: -

https://github.com/devgeeks/Canvas2ImagePlugin.git