Cordova Saving pictures to albums

Source: Internet
Author: User
Tags error code

Three ways to save a picture to an album:
One, the file operation
There are three parameters:
1) URL
2) folder name you want to create in your SD card
3) file name (you can give any name to the file)

All types of files can be downloaded by using this code.
The first step is to check the parameters and network turn state
function DownloadFile (URL, folder_name, file_name) {
if (URL = = NULL && folder_name = = NULL && file_name = = null) {
Return
}
else {
var networkstate = Navigator.connection.type;
if (networkstate = = Connection.none) {
Return
} else {
Download (URL, folder_name, file_name);
}
}
}
//second step to get Write permission and create folder
function Download (URL, folder_name, file_name) {
Window.requestfilesystem (localfilesystem.persistent, 0, filesystemsuccess, filesystemfail);
function Filesystemsuccess (fileSystem) {
var download_link = encodeURI (URL);
ext = download_link.substr (Download_link.lastindexof ('. ') + 1);
var DirectoryEntry = Filesystem.root; To get root path of directory
Directoryentry.getdirectory (folder_name, {create:true, exclusive:false}, Ondirectorysuccess, OnDirectoryFail);
var rootdir = Filesystem.root;
var fp = Rootdir.fullpath;
FP = fp + "/" + folder_name + "/" + file_name + "." + ext;
Download function call
Filetransfer (Download_link, FP);
}

function Ondirectorysuccess (parent) {
Directory created successfuly
}

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);
}
}
//Third step download file to create folder

Function Filetransfer (Download_link, FP) {
var filetransfer = new Filetransfer ();
Filetransfer.download (Download_link, FP,
Function (entry) {
alert ("Download complete:" + Entry.fullpath);
},
Function (Error) {
//download abort errors or Download failed errors
Alert ("Download error Source" + Error.source);
//alert ("Download error target" + error.target);
//alert ("Upload error code" + Error.code);
}
);
}
Second, use the file plug-in PhoneGap (this way downloaded on the Android download completed after downloading the picture needs to restart the phone to see, not recommended)

var url = ' Http://image_url ';
    var filePath = ' local/path/to/your/file ';
    var filetransfer = new Filetransfer ();
    var uri = encodeURI (URL);
    Filetransfer.download (
        Uri,
        FilePath,
        function (entry) {
            console.log ("Download complete:" + Entry.fullpath);
        },
        function (Error) {
            console.log ("Download error source" + Error.source);
            Console.log ("Download error target" + error.target);
            Console.log ("Upload error code" + Error.code);
        },
        false,
        {
            headers: {
            }
        }
    );

Third, the use of Canvas2imageplugin (recommended use)
Cordova Plugin Add Https://github.com/devgeeks/Canvas2ImagePlugin.git

    $scope. Downphoto = function (Photopath) {var pictrueurl = encodeURI (Photopath);
        function Saveimagetophone (URL, success, error) {var canvas, context, Imagedataurl, imageData;
        var img = new Image ();
          Img.onload = function () {canvas = document.createelement (' canvas ');
          Canvas.width = Img.width;
          Canvas.height = Img.height;
          context = Canvas.getcontext (' 2d ');
          Context.drawimage (IMG, 0, 0);
            try {imagedataurl = Canvas.todataurl (' image/jpeg ', 1.0);
            ImageData = Imagedataurl.replace (/data:image\/jpeg;base64,/, "); Cordova.exec (Success, error, ' Canvas2imageplugin ', ' Saveimagedata
          Tolibrary ', [ImageData]);
          } catch (e) {error (E.message);
        }
        };
        try {img.src = URL; } catch (e) {error (e.meSsage);
      }} var success = function (msg) {//download succeeded};
      var error = function (err) {//download failed};
    Saveimagetophone (Photopath, success, error); }
  })
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.