在firefox OS中将图像文件保存在sdcard中

时间:2014-03-27 07:01:12

标签: javascript firefox-os

我正在尝试在SD卡中保存图像。我正在关注this文档。

$('.btnSave').on('click', function () {

        var imageRawData = canvas.toDataURL("image/png") ;
        var sdcard = navigator.getDeviceStorage("sdcard");
        var file = new Blob([imageRawData], { type: "image/png" });
        var request = sdcard.addNamed(file, "FilertedImage.png");

                request.onsuccess = function () {
                var name = this.result;
                console.log('File "' + name + '" successfully wrote on the sdcard storage area');

                }
                request.onerror = function (e) {
                console.log('Unable to write the file: ' + e.target.error.name);

                }
    }); 

在文档中,我发现“图片只接受带有效image mime type的Blob。那么如何使用javascript将imageRawData转换为有效的图像mime类型。

2 个答案:

答案 0 :(得分:2)

我已完成以下操作 - 保存然后分享:

 function saveAndSend(blob) {
     var sdcard = navigator.getDeviceStorage("sdcard");
     var request = sdcard.addNamed(blob, "test/mycanvas.png");

     //could just share the blob instead of saving
     request.onsuccess = function () {
         var sharingImage = new MozActivity({
             name: "share",
             data: {
                 type: "image/*",
                 number: 1,
                 blobs: [blob],
                 filenames: ["mycanvas.png"],
                 filepaths: ["test/mycanvas.png"]
             }
         });
     }

     // An error could occur if a file with the same name already exist
     request.onerror = function () {
         alert('Unable to write the file: ' + this.error.name);
     }

 }


 var cnv = document.getElementById('myCanvas');
 cnv.toBlob(function (blob) {

     //var sdcard = navigator.getDeviceStorage("pictures");
     var sdcard = navigator.getDeviceStorage("sdcard");
     var request = sdcard.delete("test/mycanvas.png");
     //try to delete in case it exists
     request.onsuccess = function () {
         saveAndSend(blob);
     }

     request.onerror = function () {
         saveAndSend(blob);
     }


 });

答案 1 :(得分:1)

您的应用还需要确保它具有相应的设备存储权限。

有关示例,请参阅:https://github.com/mozilla-b2g/gaia/blob/master/dev_apps/ds-test/manifest.webapp#L13。 ds-test是我为测试设备存储中的内容而编写的测试应用程序。