Languages

Menu
Sites
Language
How to save canvas image to device photo gallery

Hi,

I am working on the image editing app. I want to save an image to device gallery. Right now whatevever code I have used, I can Able to save it to file manager but I want to save it to device gallery.

Please suggest me the way to save an image to device gallery.

Thanks

 

Responses

7 Replies
Seoghyun Kang

Hello,

 

You wrote the following comments.

"I can Able to save it to file manager but I want to save it to device gallery."

 

I understand that you already know how to save the canvas image to the file system. Is it right?

 

If it is true, you just need to save the image to the '/opt/usr/media/Images'. Then you can see the image in the gallery.

If you need the more code, please let me know,

 

Thanks.

Seoghyun Kang

Please refer the following sample code.

 

function save() {    
    var canvas = document.getElementById("canvas_id"),
        data = canvas.toDataURL().replace('data:image/png;base64,',''),
        filename = "IMAGE_" + Date.now().toString() + ".png";
               
        onsuccess = function(dir) {
              var file = dir.createFile(filename);
              
              file.openStream("w", function (stream) {
                  stream.writeBase64(data);
                  stream.close();
                  tizen.content.scanFile(file.toURI());
              }, onerror, "UTF-8");
        },

        onerror = function(e) {
            console.log(e.name + " : " + e.message)
        };
    
    tizen.filesystem.resolve("file:///opt/usr/media/Images", onsuccess, onerror, "rw");
}

 

Shweta Thakar

It works perfect. Thank you.

Palitsyna

Hello,

you can find useful information about FileSystem here:

Tutorial: https://developer.tizen.org/development/tutorials/web-application/tizen-features/inputoutput/filesystem

API Reference: https://developer.tizen.org/dev-guide/2.3.1/org.tizen.web.apireference/html/device_api/mobile/tizen/filesystem.html

 

also, you should add http://tizen.org/privilege/content.write and http://tizen.org/privilege/filesystem.read privileges to your config.xml file.

AVSukhov

Hello,

Try following code:

var dirName = "images";
var dir = null;
var file =  null;
if ("undefined" !== typeof(tizen)) {
    tizen.filesystem.resolve(dirName.toLowerCase(), function(dir) {
        dir = dir;
    }, function(err) {
        alert("Could not resolve directory " + fs.dirName + ": " + err.message);
    }, "rw");
}
try {
    dir.createFile("test.png");
    }
catch (exc) {
    console.log("Could not create file " + exc.message);
}
try {
    file = dir.resolve("test.png");
}
catch (exc) {
    console.log("Could not resolve file " + exc.message);
}

file.openStream('w', function(fileStream) {
    try {
        var dataURL = drawing.getCanvasRender().toDataURL().replace("data:image/png;base64,", "");
        fileStream.writeBase64(dataURL);
        fileStream.close();

        tizen.content.scanFile(file.toURI(), function() {
            console.log('scanning is completed. database refreshed');
        }, function(err) {
            console.log('The following error occurred: ' + err.name);
        });

    } catch (exc) {
        console.log("Could not write to file: " + exc.message);
    }
    }, function(err) {
        console.log("Could not open file stream: " + err.message);
    });
}

 

Palitsyna

Hello,

the way proposed by AVSukhov is working well. But don't foreget to add Tizen Privileges:

    <tizen:privilege name="http://tizen.org/privilege/filesystem.read"/>
    <tizen:privilege name="http://tizen.org/privilege/content.write"/>

Add this lines to your config.xml file.

AVSukhov

Hello,

Yes, info on the required privileges can be founc in API References documentation for particular API. In out case:

Filesystem API: https://developer.tizen.org/dev-guide/2.3.1/org.tizen.web.apireference/html/device_api/mobile/tizen/filesystem.html

Content API: https://developer.tizen.org/dev-guide/2.3.1/org.tizen.web.apireference/html/device_api/mobile/tizen/content.html