cordova儲存圖片到相簿

來源:互聯網
上載者:User

將圖片儲存到相簿的三種方式:
一、檔案操作
有三個參數:
1)URL
2)檔案夾名稱你想在你的SD卡的建立
3)檔案名稱(你可以給任何名稱的檔案)

所有類型的檔案可以通過使用此代碼下載。
//第一步檢查參數和網路轉態
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);
}
}
}
/ /第二步得到寫入權限和建立檔案夾
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);
}
}
/ /第三步下載檔案到建立檔案夾

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);
}
);
}
二、使用PhoneGap的file外掛程式(此種方式下載的在安卓上面下載完成後看不到圖片需要重啟手機才能看到,不推薦使用)

 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: {            }        }    );

三、使用Canvas2ImagePlugin(推薦使用)
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',              'saveImageDataToLibrary',              [imageData]            );          }          catch (e) {            error(e.message);          }        };        try {          img.src = url;        }        catch (e) {          error(e.message);        }      }      var success = function (msg) {      //下載成功      };      var error = function (err) {       //下載失敗      };      saveImageToPhone(photoPath, success, error);    }  })
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.