Phonegap - Save image from url into device photo g

2019-01-01 15:30发布

I'm developing phonegap application and I need to save Image from url to the Device Photo Gallery.

I can't find at the Phonegap Api a way for doing it and Also I didn't find phonegap plugin for that.

I need it to work with Iphone & Android

Thanks a lot!

9条回答
回忆,回不去的记忆
2楼-- · 2019-01-01 15:41

This can be done using phone gap file plugin:

 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: {
                "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA=="
            }
        }
    );
查看更多
查无此人
3楼-- · 2019-01-01 15:41

Simplest approach

If you are ok with it being in the download's folder do the following

  1. Install In-App browser plugin

    cordova plugin add cordova-plugin-inappbrowser
    
  2. Create a download button with

    onclick="window.open("Image_URL", '_system');
    

Not only will this download the image it will offer to open the image in the corresponding app or browser.

查看更多
听够珍惜
4楼-- · 2019-01-01 15:41

I'm currently working on cordova-plugin-photo-library.

It can save image that given by url (file:// or data:). Works on ios and android, jpeg/png/gif:

cordova.plugins.photoLibrary.saveImage(url, 'My album', function () {}, function (err) {});

查看更多
梦寄多情
5楼-- · 2019-01-01 15:44

The latest version of Cordova (3.3+), the newer (1.0.0+) version of File uses filesystem URLs instead of the file path. So, to make the accepted answer work with the newer version in the FileSystemSuccess function change the line:

var fp = rootdir.fullPath; 

to:

var fp = rootdir.toURL(); 
查看更多
零度萤火
6楼-- · 2019-01-01 15:48

Another easy way is to use the Cordova/Phonegap plugin Canvas2ImagePlugin. Install it and add the following function to your code which is based on getImageDataURL() by Raul Sanchez (Thanks!).

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

Use it like this:

var success = function(msg){
    console.info(msg);
};

var error = function(err){
    console.error(err);
};

saveImageToPhone('myimage.jpg', success, error);
查看更多
大哥的爱人
7楼-- · 2019-01-01 15:48

I cleaned-up and wrapped the code suggested by Suhas above - the accepted answer in an angular service so that it can easily be used in other application. You can find the snipet here.

To use it, you include the script in app.js (and your index.html file) then:

angular.module('myApp.controllers.whatever', [])
    .controller('WhateverController', function ($scope, SaveToGalleryService) {

    $scope.savePhoto = function(photoUrl, folderName, fileName, callback) {
        var fileName = new Date().getTime();
        SaveToGalleryService.saveToGallery(photoUrl, "Kiddiz.me", fileName, function saveSuccess(res) {
            console.log("Photo ", photoUrl, "photo saved", res);
            if (callback) {
                callback(true, res);
            }
        }, function saveFail () {
            console.log("Photo ", photoUrl, "failed to save photo");
            if (callback) {
                callback(false);
            }
        });
    }
});
查看更多
登录 后发表回答